Here I am going to show in step by step how to set connection string of database in asp.net core project using entity framework core.

Step 01 (Create project):
Create an asp.net core web application named "EmployeeProject". Press Ok.



Now select web application and click on ok.

Step 02 (Create object class):
Create a folder named "Models" and create a object class named "Employee.cs".




using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

 public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Designation { get; set; }
        public string Skills { get; set; }
        public int ProjectId { get; set; }
    }

Step 3 (Create context class) :

Create a context class named "EmployeeContext.cs" under models folder.

    using Microsoft.EntityFrameworkCore;

    public class EmployeeContext : DbContext
    {
        public EmployeeContext(DbContextOptions<EmployeeContext> options) : base(options)
        {
        }

        public DbSet<Employee> Employee { get; set; }
    }


Step 4 (Add NuGet Packages) :

Open "project.json".

Add 3 NuGet Packages in "dependencies" part of project.json file like below.

1. Microsoft.EntityFrameworkCore
2. Microsoft.EntityFrameworkCore.SqlServer
3. Microsoft.EntityFrameworkCore.Tools



Add "Microsoft.EntityFrameworkCore.Tools" in tools part.


Step 5 (Add Services) :

Open Startup.cs class, and modify ConfigureServices method.

        public void ConfigureServices(IServiceCollection services)
        {
            //Register Context with dependency Injection
            var configurationSection = Configuration.GetSection("ConnectionStrings:DefaultConnection");
            services.AddDbContext<EmployeeContext>(options => options.UseSqlServer(configurationSection.Value));

            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddMvc();

        }



Step 6 (Add connection string) :

Open "appsettings.json".



Step 7 (Add migration) :


Now I am going to add a migration,Select tools => NuGet Package Manager => Package manager console. Like Below :








Write add-migration firstMigration and click enter.










After migration done we can see a migration folder and class like below.



Step 8 (Create database) :

Now right "update-database -verbose" but press enter.



After "done" this operation, open SQL server and we will see a database named "Employee_AspDotNetCore" created.