在 Entity Framework Core 中创建迁移时如何配置 DbContext? [英] How do you configure the DbContext when creating Migrations in Entity Framework Core?

查看:29
本文介绍了在 Entity Framework Core 中创建迁移时如何配置 DbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在使用实体框架的迁移命令时配置/引导依赖注入?

Is there way that dependency injection can be configured/bootstrapped when using Entity Framework's migration commands?

Entity Framework Core 支持对 DbContext 子类的依赖注入.该机制包括允许在 DbContext 之外配置数据访问.

Entity Framework Core supports dependency injection for DbContext subclasses. This mechanism includes allowing for configuration of data access outside of of the DbContext.

例如,以下将配置 EF 以使用从 config.json

For example, the following would configure EF to persist to a SQL server using a connection string retrieved from config.json

ServiceCollection services = ...

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

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

However, the migrations commands do not know to execute this code so Add-Migration will fail for lack of a provider or lack of a connection string.

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

Migrations can be made to work by overriding OnConfiguring within the DbContext subclass to specify the provider and configuration string, but that gets in the way when different configuration is desired elsewhere. Ultimately keeping my the migration commands and my code both working becomes undesirably complex.

注意:我的 DbContext 与使用它的入口点位于不同的程序集中,并且我的解决方案有多个启动项目.

Note: My DbContext lives in a different assembly than the entry point that uses it and my solution has multiple start-up projects.

推荐答案

由于 @bricelam 评论此功能尚未实体框架 7 中存在.此缺失的功能由 GitHub 问题跟踪aspnet/EntityFramework#639

As @bricelam commented this functionality does not yet exist in Entity Framework 7. This missing functionality is tracked by GitHub issue aspnet/EntityFramework#639

与此同时,我发现更简单的解决方法是利用全局状态而不是子类化的麻烦.通常不是我的第一个设计选择,但目前效果很好.

In the mean time, the easier workaround I found was to utilize a global state rather than hassle with subclassing. Not usually my first design choice but it works well for now.

在 MyDbContext 中:

In 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( "<Your Connection String Here>" );
    }
}

Startup.ConfigureServices()中.

public IServiceProvider ConfigureServices( IServiceCollection services )
{
    MyContext.isMigration = false;

    var configuration = new Configuration().AddJsonFile( "config.json" );
    services.AddEntityFramework( configuration )
        .AddSqlServer()
        .AddDbContext<MyDbContext>( config => config.UseSqlServer() );
    // ...
}

(就我而言,配置代码实际上存在于 Autofac 模块中.)

(The configuration code actually lives in an Autofac Module in my case.)

这篇关于在 Entity Framework Core 中创建迁移时如何配置 DbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆