Proper way to set mutiple AddDbContext() options?

asp.net-core-2.0 c# entity-framework-core

Question

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?

1
1
6/29/2019 5:14:15 PM

Accepted Answer

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")));
}
2
6/30/2018 8:21:07 PM

Popular Answer

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow