C# 设置多个AddDbContext()选项的正确方法?

C# 设置多个AddDbContext()选项的正确方法?,c#,entity-framework-core,asp.net-core-2.0,C#,Entity Framework Core,Asp.net Core 2.0,我将我的站点解决方案分为多个项目,其中一个项目具有数据库类。当我尝试生成第一次迁移时,出现以下错误: 实体框架核心2.0.3-rtm-10026已初始化“SiteDBContext” 使用具有以下选项的提供程序“Microsoft.EntityFrameworkCore.SqlServer”: 没有 使用DbContextOptionsBuilder更改迁移程序集。例如。 选项。使用SQLServer(连接,b=> b、 迁移程序集(“”)。默认情况下,迁移 assembly是包含DbConte

我将我的站点解决方案分为多个项目,其中一个项目具有数据库类。当我尝试生成第一次迁移时,出现以下错误:

实体框架核心2.0.3-rtm-10026已初始化“SiteDBContext” 使用具有以下选项的提供程序“Microsoft.EntityFrameworkCore.SqlServer”: 没有

使用DbContextOptionsBuilder更改迁移程序集。例如。 选项。使用SQLServer(连接,b=> b、 迁移程序集(“”)。默认情况下,迁移 assembly是包含DbContext的程序集

使用将目标项目更改为迁移项目 包管理器控制台的默认项目下拉列表,或按 从包含迁移的目录执行“dotnet ef” 项目

目标项目“”与迁移不匹配 程序集“”。要么改变你的目标项目,要么改变它 您的迁移程序集

使用DbContextOptionsBuilder更改迁移程序集。例如。 选项。使用SQLServer(连接,b=> b、 迁移程序集(“”)。默认情况下,迁移 assembly是包含DbContext的程序集。改变你的目标 使用包管理器将项目导入到迁移项目 控制台的默认项目下拉列表,或通过执行“dotnet ef” 从包含迁移项目的目录中

我想在我的连接字符串命令旁边添加这个
migrationassembly
选项,但我不知道该怎么做

我目前的代码是:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB")));
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“ffinfo数据库”));
}
我试过:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), options.MigrationsAssembly("FFInfo.DAL")));
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“FFInfo.db”)、options.MigrationsAssembly(“FFInfo.DAL”));
}
但是我得到了错误

DbContextOptionsBuilder没有为MigrationAssembly创建定义


在第二个选项中添加的正确方法是什么?

您非常接近,但是第二级配置是通过
操作来完成的,而不是通过原始的
操作选项来完成的

因此,请使用以下命令:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), sqlServerOptions => sqlServerOptions.MigrationsAssembly("FFInfo.DAL")));
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“FFInfo数据库”)、sqlServerOptions=>sqlServerOptions.migrationassembly(“FFInfo.DAL”));
}

我也有同样的问题,但解决方法却大不相同。我的ConfigureServices与上面的答案几乎相同,但我仍然遇到相同的问题:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<AppDbContext>(option =>
        { 
           option.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")); //I set this on appsettings.json
        });
    }
dotnet ef migrations add initialcreate -s ..\Core.Web\Core.Web.csproj