Skip to content

Scalable scenario to configuring Entity Framework DbContext(ASP.Net Core 3.x & MSTest) and creating a flexible Startup class

Notifications You must be signed in to change notification settings

TarekNajem04/ConfigureEFDbContext

Repository files navigation

ConfigureEFDbContext

Scalable scenario to configuring Entity Framework DbContext(ASP.Net Core 3.x & MSTest) and creating a flexible Startup class We will show different ways to configure your application using the ASP.NET CORE 3.x

The Original post on TarekNajem04@Medium

We will show different ways to configure our application using the ASP.NET CORE 3.x and creating a flexible Startup class.

Background

When we are configuring the DbContext in an ASP.NET Core web application, we typically use AddDbContext extension method as follows:

services.AddDbContext<xxxDbContext>(dbContextOptionsBuilder =>
        dbContextOptionsBuilder.UseSqlServer(Configuration.GetConnectionString("The name of the connection string in the configuration file.")
    ));
// Or
services.AddDbContext<xxxDbContext>((serviceProvider, dbContextOptionsBuilder) =>
{
    var service = serviceProvider.GetService<xxx>();
    dbContextOptionsBuilder.UseSqlServer(Configuration.GetConnectionString("The name of the connection string in the configuration file.");
});

If we take a closer look at the parameter of the AddDbContext extension method, we’ll find that is an action, and through un Action, we can encapsulate a method , delegate, in-line delegate, lambda or etc.
In this case, the Action must construct the DbContext options.
What interests us most is configuring the DbContext according to the our configuration ‘{environment}settings.json’.

How do we implement this scenario and why?

The answer to the question why would we do this:
We want to dramatically simplify and improve the experience for configuring DbContext and make them truly composable, where we can try to create a flexible scenario that automatically configures DbContext so that it can encapsulate a complete feature and provide an instant utility without having to go through various steps on how to manually configure it in different places in the Startup configuration class.

About

Scalable scenario to configuring Entity Framework DbContext(ASP.Net Core 3.x & MSTest) and creating a flexible Startup class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages