Code First Connection By Migration .NET Core

Add package by right clicking on the project. Click on manage nuget packages, browse package, search microsoft.entityframeworkcore.sqlserver , install it, search microsoft.entityframeworkcore.tool, , click ok and click accept on pop up.
This package will be added in packages. Add DataBaseContext.cs class in model inherit DbContext in the method class of DataBaseContext and use namespace. Microsoft.EntityFrameworkCore forDbContext Definition makes a constructor for connectivity with the database.
  1. public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)  
  2. {  
  3. }  
In database context class add a class in the model for the table you want to make in the database. You can use a prop and double tab. For auto creating a property in class assign primary key here only on the property of class as an attribute with keyword [Key]. Use namespace System.ComponentModel.DataAnnotations for key attribute definition now link this table as DbSet type of property in DataBaseContext class LIKE this,
  1. public DbSet<Student> students { getset; }   
Now use connection string in appsetting.json below "AllowedHosts": "*",
  1. "ConnectionStrings": {  
  2.    "DatabaseConnection""Data Source=DESKTOP-DCDA4OG\\SQLEXPRESS; Initial Catalog=CodeFirst; Integrated Security=True;"  
  3. }   
Now create a new database in SQL server rest of table will automatically be created by the project itself

  1. string connection = Configuration.GetConnectionString("DataBaseConnection");  
  2. services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));  
in Configuresrevices in startup.cs use namespace microsoft.entityframeworkcore for usesqlserver connectivity keyword now add controller lets say with name usercontroller add model class add datacontext class click add you can auto create add update delete list by scaffolding now do migration,

select tools - > nugetpackage manager -> package manager console

write Add-Migration InitialCreate and enter

Write update-database enter

Now run controllers.

Next Recommended Reading Dynamic Connection String In .NET Core