Posts

Code First Method in C# .NET 8 Entity Framework Core

Image
There are 3 approach to deploy data from database to api One is Code First.in here we first create data model and create and update database later   Create Project with Employee Controller Here we create the Data Model  public class Employee_Personal { [Key]     public int Id { get; set; } [Required] [MaxLength(50)] public string Employee_First_Name{ get; set; } [Required] [MaxLength(50)] public string Employee_Last_Name { get; set; } [Required] public DateTime DOB{ get; set; } [Required] [MaxLength(30)] public string Position { get; set; } } Then get the connection string  from  App setting json  and Inject to Program.cs class here is connection string  "ConnectionStrings": {   "ConStrEmp": "Server=LAPTOP-2OTFH16P\\SQLEXPRESS;Database=EmployeeDb;User Id=sa;password=123;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True;" },  here is how to add Program.cs class builder.Services....
Image
  Create Context class optional constructor in fly   Go to Visual Studio 2022 and Right click on the project Add->Class     give a Name  as ContactDbContext   Inherit it   from     DbContext                       Then right click on the class name Proess Ctrl + (.)Period Symbol select Generate Constructor 'ContactDbContext(options)' Format:Contrctuctor 'ClassName(options)' then it will create a optional constructor automatically   you can press ctor snippet to auto generate primary constructor here is the video link https://www.youtube.com/watch?v=zUDN5MzQuj8

Create Web Api with Entity Framework Core DB First

Image
DBFirst Approach always create database first and then automatically create model from db First Create Database and Create Table with Any Name       Eg:EmployeePersonalData                Then  go to appsettings.json and put the connection string                     "ConStr": "Server=LAPTOP-2OTFH16P\\SQLEXPRESS;Database=EmployeeDb;User Id=sa;password=123;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True;" Server: SQL server Name Database:Db Name Trusted_Connection need for HTTPS TrustServerCertificate=True also compulsory for HTTPS Then Right Click on Nuget Packages and install below packages below 3 packages are compulsory and should be same version and stable version              Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Then Execute...