Matthew Verstraete
Matthew Verstraete

Reputation: 6781

Proper way to set multiple AddDbContext() options?

I have my site solution split into multiple projects, one of which has the classes for the Database. When I tried to generate the first migration I get the following error:

Entity Framework Core 2.0.3-rtm-10026 initialized 'SiteDBContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None

Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("")). By default, the migrations assembly is the assembly containing the DbContext.

Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

Your target project '' doesn't match your migrations assembly ''. Either change your target project or change your migrations assembly.

Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

I want to add thie MigrationsAssembly option along side my connection string command but can't figure out exactly how to do it.

My current code is:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB")));
    }

I tried:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), options.MigrationsAssembly("FFInfo.DAL")));
    }

But I get the error

DbContextOptionsBuilder does not cainta a definition for MigrationsAssembly

What is the proper way to add in this second option?

Upvotes: 5

Views: 9947

Answers (2)

Willy David Jr
Willy David Jr

Reputation: 9151

I have the same issue but the fix is quite different. My ConfigureServices is almost the same as the answer above but still I am experiencing the same issue:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<AppDbContext>(option =>
        { 
           option.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")); //I set this on appsettings.json
        });
    }

The problem is that I am executing the code below on my solution directory which obviously doesn't contains EF Core files and not on the directory that needs to populate the Migration files (Data Access Layer folder). Your case might be different but be sure to change your folder where your DbContext file resides.

So I changed my directory to my Data Access Layer folder, then that's the time I execute the code below since my startup project is on Core.Web folder and this is also the folder that contains my Startup.cs file. (-s means startup)

dotnet ef migrations add initialcreate -s ..\Core.Web\Core.Web.csproj

Upvotes: 1

Camilo Terevinto
Camilo Terevinto

Reputation: 32088

You are quite close, but the second level of configuration is done through an Action<SqlServerDbContextOptionsBuider> and not through the original Action<DbContextOptionsBuilder> options parameter.

So, use this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), sqlServerOptions => sqlServerOptions.MigrationsAssembly("FFInfo.DAL")));
}

Upvotes: 2

Related Questions