In this article I will explain with an example, how to set Entity Framework Core 3 DbContext Connection String.
Note: For beginners in ASP.Net Core 3, please refer my article ASP.Net Core 3: Hello World Tutorial with Sample Program example.
 
 
Installing Entity Framework Core
For more details on installing EntityFrameworkCore, please refer my article .Net Core 3: Install Entity Framework Core 3.
 
 
Adding the Connection String inside AppSettings.json
The following Connection String setting has been added in the AppSettings.json file.
{
  "ConnectionStrings": {
    MyConn""Data Source=.\\SQL2022;Initial Catalog=Northwind;Integrated security=true"
  }
}
 
 
Database Context
1. Now you will need to add a new class to your project by right clicking on the Solution Explorer and then click on Add and then New Item option from the Context Menu.
2. Inside the class, first inherit the EntityFrameworkCore namespace and then inherit the DbContext class.
Then using Dependency Injection, a Constructor is created DbContextOptions are passed as parameter and also the Constructor of base class i.e. DbContext class is inherited.
using Microsoft.EntityFrameworkCore;
 
namespace EF_Core_3_MVC
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
    }
}
 
 
Setting DbContext Connection String in Startup class
Inside the Startup class, the IConfiguration is injected in the Startup class and assigned to the private property Configuration.
Then, the Connection String is read from the AppSettings.json file and is used to add the DbContext service.
Note: For more details on reading Connection String inside the Startup class, please refer my article .Net Core 3: Read Connection String inside Startup.cs from AppSettings.json file.
 
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
 
namespace EF_Core_3_MVC
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
 
        public IConfiguration Configuration { get; }
 
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            string conStr = this.Configuration.GetConnectionString("MyConn");
            // Setting DbContext Connection String.
            services.AddDbContext<DBCtx>(options => options.UseSqlServer(conStr));
        }
 
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
 
Note: For more details, please refer my article ASP.Net Core 3.1: Simple Entity Framework Tutorial with example.
 


Other available versions