[Derived from the content of the article in the Microsoft Doc ]
Some EF Core Tools command (such as migration ) require the design to create a derivative when DbContext
instance, in order to collect about the application entity type and details of how the database schema mapped to.
Design configuration DbContext
Configuration DbContextOptions
DbContext
You must have DbContextOptions
an instance to perform the work.
Examples include DbContextOptions follows:
- Database provider, generally selected by invoking a method (UseSqlServer or UseSqlite).
- Examples of the database must connection string or identifier, is usually passed to the above method provided by the parameter selection.
- Provider-level optional behavior selector.
- EF Core behavior selector
Configuration example:
optionsBuilder .UseSqlServer(connectionString, providerOptions=>providerOptions.CommandTimeout(60)) .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
The constructor configured DbContextOptions DbContext There are no parameters are two types: setting constructor parameter passing, a method is provided OnConfiguring override.
-
Constructor parameters
There argument constructor, using the parameters passed to the DbContextOptions DbContext
1 public class BloggingContext : DbContext 2 { 3 public BloggingContext(DbContextOptions<BloggingContext> options) 4 : base(options) 5 { } 6 7 public DbSet<Blog> Blogs { get; set; } 8 }
-
OnConfiguring
No argument constructor, override OnConfiguring method.
1 public class BloggingContext : DbContext 2 { 3 public DbSet<Blog> Blogs { get; set; } 4 5 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 6 { 7 optionsBuilder.UseSqlite("Data Source=blog.db"); 8 } 9 }
DbContext create design
Migration tools are designed to create DbContext a variety of ways:
- From the application service creation.
- Created using a constructor with no arguments.
- Creating the plant from design.
-
From an application service creation
If you start the project using ASP.NET Core Web host or .Net Core generic host , these tools will attempt to provide a service application program from the get DbContext object.
Tools first try by calling Program.CreateHostBuilder()
, calling Build()
, and then access the Services
property to get the service provider. DbContext
Its constructor of any dependencies are required for the service provider is registered in a service application.
1 public class Program 2 { 3 public static void Main(string[] args) 4 => CreateHostBuilder(args).Build().Run(); 5 6 // EF Core uses this method at design time to access the DbContext 7 public static IHostBuilder CreateHostBuilder(string[] args) 8 => Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults( 10 webBuilder => webBuilder.UseStartup<Startup>()); 11 } 12 13 public class Startup 14 { 15 public void ConfigureServices(IServiceCollection services) 16 => services.AddDbContext<ApplicationDbContext>(); 17 } 18 19 public class ApplicationDbContext : DbContext 20 { 21 public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 22 : base(options) 23 { 24 } 25 }
-
Created using a constructor with no arguments
If the service provider can not be obtained from the application DbContext, the tool will find items in the DbContext
derived type. Then, they try to create an instance using the constructor without parameters. This mode requires DbContext override OnConfiguring method DbContextOptions disposed in the process.
-
Creating the plant from design
By implementing IDesignTimeDbContextFactory<TContext>
an interface to tell the tool how to create DbContext: If the derived DbContext
project the same project or in a startup project application to find the class that implements this interface, these tools will bypass the creation and use of other methods DbContext the design of the plant. If required in a manner different runtime configuration DbContext design, DbContext
the design is particularly useful when the plant.
1 using Microsoft.EntityFrameworkCore; 2 using Microsoft.EntityFrameworkCore.Design; 3 using Microsoft.EntityFrameworkCore.Infrastructure; 4 5 namespace MyProject 6 { 7 public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext> 8 { 9 public BloggingContext CreateDbContext(string[] args) 10 { 11 var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>(); 12 optionsBuilder.UseSqlite("Data Source=blog.db"); 13 14 return new BloggingContext(optionsBuilder.Options); 15 } 16 } 17 }