This article transferred from: Https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
Note
This documentation are for EF Core. For ef6.x, see Entity Framework 6. +
This article shows patterns is configuring a DbContext with DbContextOptions . Options is primarily used to select and configure the data store. +
Configuring Dbcontextoptions
DbContextMust has an instance of in DbContextOptions order to execute. This can is configured by overriding OnConfiguring , or supplied externally via a constructor argument. +
If Both is used, was OnConfiguring executed on the supplied options, meaning it's additive and can overwrite options supplied to The constructor argument. +
Constructor argument
Context code with constructor+
copyc#
PublicClassBloggingcontext:dbcontext{public bloggingcontext (dbcontextoptions<bloggingcontext> Options Base ( Optionspublic DbSet< Blog> Blogs {get; set;}
Tip
The base constructor of DbContext also accepts the non-generic version of DbContextOptions . Using the non-generic version is not a recommended for applications with multiple context types. +
Application code to initialize from constructor argument+
copyc#
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();optionsBuilder.UseSqlite("Filename=./blog.db");using (var context = new BloggingContext(optionsBuilder.Options)){ // do stuff}
Onconfiguringwarning
OnConfiguringOccurs last and can overwrite options obtained from DI or the constructor. This approach does isn't lend itself to testing (unless you target, the full database). +
Context code with onconfiguring+
copyc#
PublicClassBloggingcontext:dbcontext{Public dbset<blog> Blogs {Getset;} protected override void OnConfiguring (DbContextOpt Ionsbuilder optionsbuilder< Span class= "hljs-string" > "filename=./blog.db"); }}
Application code to initialize with "onconfiguring"+
copyc#
using (var context = new BloggingContext()){ // do stuff}
Using DbContext with Dependency injection
EF supports using with DbContext a dependency injection container. Your DbContext type can be added to the service container by using AddDbContext<TContext> . +
AddDbContextWould add make both your DbContext type, TContext , and to the available for injection from the DbContextOptions<TContext> service container. +
See more reading below for information on dependency injection. +
Adding DbContext to Dependency injection+
copyc#
< Span class= "Hljs-keyword" >public void configureservices (iservicecollection services "filename=./blog.db"));
This is requires adding a constructor argument to your DbContext type that accepts DbContextOptions . +
Context Code+
copyc#
PublicClassBloggingcontext:dbcontext{public bloggingcontext (dbcontextoptions<bloggingcontext> Options Base ( Optionspublic DbSet< Blog> Blogs {get; set;}
Application code (in ASP. NET Core)+
copyc#
public MyController(BloggingContext context)
Application code (using serviceprovider directly, less common)+
copyc#
using (var context = serviceProvider.GetService<BloggingContext>()){ // do stuff}var options = serviceProvider.GetService<DbContextOptions<BloggingContext>>();
+
Using
IDbContextFactory<TContext>
As an alternative to the options above, your may also provide an implementation of IDbContextFactory<TContext> . EF command line tools and dependency injection can use this factory to the create an instance of your DbContext. This is required in order to enable specific design-time experiences such as migrations. +
Implement this interface to enable Design-time services for the context types that does not has a public default constructor. Design-time services would automatically discover implementations of this interface that is in the same assembly as the de rived context. +
Example:+
copyc#
Using Microsoft.entityframeworkcore;Using Microsoft.EntityFrameworkCore.Infrastructure;Namespacemyproject{PublicClassBloggingcontextfactory:idbcontextfactory<bloggingcontext> {public bloggingcontext Create() { var optionsbuilder = New dbcontextoptionsbuilder< Bloggingcontext> (); Optionsbuilder.usesqlite ("filename=./blog.db"); return new Bloggingcontext (optionsbuilder.options); }}
More Reading
Read Getting Started on ASP. Information on using the EF with ASP.
Read Dependency Injection to learn more about using DI.
Read testing with InMemory for more information.
Read Understanding EF Services For more details on how EF uses dependency injection internally.
[Turn] EF Configuring a DbContext