vossad01 vossad01 - 8 days ago 6
C# Question

How do you configure the DbContext when creating Migrations in Entity Framework 7?

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

Entity Framework 7 supports dependency injection for

DbContext
subclasses. This mechanism includes allowing for configuration of data access outside of of the
DbContext
.

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() );


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.

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.

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

Answer

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.

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>" );
    }
}

In 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() );
    // ...
}

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