在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"); } }
- 应用程序在添加Oauth时重定向到Account / AccessDenied
- 我应该在哪里开始ASP.NET Core中的持久后台任务?
- 将Azure ServiceBus上的消息从.NET Core发送器发送到.NET 4.6处理程序
- 如何根据XUnit测试隔离EF InMemory数据库
- CS0433:两者中都存在“List ”类型
- unit testing.NET Standard 1.6库
- 正文的签名和方法实现中的声明不匹配
- 将ArrayPool对象提供给JsonOutputFormatter构造函数
- Azure Fluent API创建SQL Server时出错 – 缺少x-ms-request-id标头