[Turn] EF Configuring a DbContext

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.