在Entity Framework Core中创建迁移时如何配置DbContext?

在使用Entity Framework的迁移命令时,是否可以配置/引导dependency injection?

Entity Framework Core支持DbContext子类的dependency injection。 该机制包括允许在DbContext之外配置数据访问。

例如,以下内容将使用从config.json检索的连接字符串将EF配置为持久保存到SQL Server

 ServiceCollection services = ... var configuration = new Configuration().AddJsonFile( "config.json" ); services.AddEntityFramework( configuration ) .AddSqlServer() .AddDbContext( config => config.UseSqlServer() ); 

但是,迁移命令不知道执行此代码,因此由于缺少提供程序或缺少连接字符串, Add-Migration将失败。

通过在DbContext子类中重写OnConfiguring来指定提供者和配置字符串,可以使迁移工作,但是当其他地方需要不同的配置时,这会妨碍。 最终保持我的迁移命令和我的代码都工作变得非常复杂。

注意:我的DbContext与使用它的入口点不同,而且我的解决方案有多个启动项目。

正如@bricelam所评论的那样,entity framework7中还没有这个function.GitHub问题跟踪了这个缺失的functionaspnet / EntityFramework#639

与此同时,我发现更简单的解决方法是利用全局状态而不是使用子类化。 通常不是我的第一个设计选择,但它现在运作良好。

在MyDbContext中:

 public static bool isMigration = true; protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder ) { // TODO: This is messy, but needed for migrations. // See https://github.com/aspnet/EntityFramework/issues/639 if ( isMigration ) { optionsBuilder.UseSqlServer( "" ); } } 

Startup.ConfigureServices()

 public IServiceProvider ConfigureServices( IServiceCollection services ) { MyContext.isMigration = false; var configuration = new Configuration().AddJsonFile( "config.json" ); services.AddEntityFramework( configuration ) .AddSqlServer() .AddDbContext( config => config.UseSqlServer() ); // ... } 

(在我的情况下,配置代码实际上存在于Autofac模块中。)

使用IDbContextFactory

实现此接口以为没有公共默认构造函数的上下文类型启用设计时服务。 设计时服务将自动发现此接口的实现,这些实现与派生上下文在同一程序集中。

 using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; namespace MyProject { public class BloggingContextFactory : IDbContextFactory { public BloggingContext Create() { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer("connection_string"); return new BloggingContext(optionsBuilder.Options); } } } 

更多信息: https : //docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

如果您对硬编码的连接字符串不满意,请查看本文。

我知道这是一个老问题,但我使用onConfiguring方法,我没有这个问题

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString")); } 

我只是要求一个实例并在我的Startup.cs文件中运行迁移

  public void ConfigureServices(IServiceCollection services) { // ASPNet Core Identity services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("RRIdentityConnectionString"))); } 

然后在配置中:

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { var rrIdentityContext = app.ApplicationServices.GetService(); rrIdentityContext.Database.Migrate(); } 

注意:数据库没有“EnsureCreated”。 迁移应该创建它,如果它不存在,虽然它应该如何找出我不知道的权限 – 所以我创建了一个空数据库。

结合上面的答案,这对我有用

 private readonly bool isMigration = false; public MyContext() { isMigration = true; } public MyContext(DbContextOptions options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (isMigration) { optionsBuilder.UseSqlServer("CONNECTION_STRING"); } }