Dependency Injection with Entity Framework Core has an excellent feature through which this framework provides you with an object of any class that you want. So you don’t have to manually create the class object in your code. ASP.NET Core Dependency Injection In this tutorial I will teach you how to use the Dependency Injection method in . This will provide you can get the following benefits: Entity Framework Core 1. The object of ‘DbContext’ class through Dependency Injection. 2. Fetch the connection string from ‘appsettings.json’ instead of from ‘OnConfiguring()’ method of ‘DbContext’ class. Connection String inside OnConfiguring() method Normally you provide your database connection string in method of class like this: OnConfiguring() DbContext protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){if (!optionsBuilder.IsConfigured){optionsBuilder.UseSqlServer(@"Server=Smile;Database=Shop;Trusted_Connection=True;"); } } This approach is not nice since it is better to store the connection string in file. After applying the Dependency Injection in you can simply remove everything inside the method like this: appsettings.json Entity Framework Core OnConfiguring() protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){if (!optionsBuilder.IsConfigured){}} You need to do the following 3 changes as described below: 1. Create appsettings.json file to store the Database Connection String The code for file storing the value is given below: appsettings.json Database Connection String {"ConnectionStrings": {"DefaultConnection": "Server=Smile;Database=Shop;Trusted_Connection=True;"}} 2. Create appsettings.json file to store the Database Connection String In your class go and add the constructor that inherits the base class. Also remove the connection string from method. The code of the class should be: DbContext OnConfiguring() DbContex public class ShopContext : DbContext{public ShopContext(DbContextOptions<ShopContext> options) : base(options) { }public DbSet<Products> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { } } 3. Add DbContext class as a service in Startup.cs class Change the class to get the object of namespace in it’s Constructor. Startup.cs IConfiguration Microsoft.Extensions.Configuration Inside the constructor set the value of a public property of type with the value of object given in it’s parameter. IConfiguration IConfiguration The code for this is: public Startup(IConfiguration configuration){Configuration = configuration;} public IConfiguration Configuration { get; } This change will help you to read the file in the class. appsettings.json Startup.cs Next, add the class as a service inside the method. See the below code: DbContext ConfigureService() services.AddDbContext<ShopContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"])); The updated code of the Startup class should be: public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ShopContext>(options => options.UseSqlServer(Configuration\["ConnectionStrings:DefaultConnection"\])); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); app.UseDeveloperExceptionPage(); app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } Get DbContext object in Controller using Dependency Injection Now you are ready and let me show how to get the class object in the Constructor using . All you have to do is to add the class object in the constructor of the Controller, and set a public property value to it. DbContext Dependency Injection DbContext You can learn all about Dependency Injection feature from this tutorial — Dependency Injection in ASP.NET Core You add the below code to the Constructor: private ShopContext shopContext; public HomeController(ShopContext sc){shopContext = sc;} Now you are ready to use Entity Framework Core to communicate with the database. To Read Records from the database: shopContext.Teacher; To Create records in the database: shopContext.Products.Add(product);shopContext.SaveChanges(); See the updated code of the Controller which also lists the action methods that reads and inserts records. public class HomeController : Controller{private ShopContext shopContext;public HomeController(ShopContext sc){shopContext = sc;} public IActionResult Index() { return View(shopContext.Teacher); } public IActionResult Create() { return View(); } \[HttpPost\] public IActionResult Create\_Post(Products product) { if (ModelState.IsValid) { shopContext.Products.Add(product); shopContext.SaveChanges(); return RedirectToAction("Index"); } else return View(); } } Conclusion I hope you like this tutorial on . It is just one step to improve your code. If you like this tutorial then please give me , and do for 1 tutorial on Web Development every week. Entity Framework Core with Dependency Injection some claps follow me Also check my other article in HACKERNOON — 7 Common Web Development problems which every developer from Beginners to Experts should know [with multiple solutions]