ASP.NET Core多个dbcontext无法添加迁移

时间:2017-10-12 13:02:06

标签: c# asp.net-core entity-framework-core asp.net-core-2.0 ef-core-2.0

我有2个DbContexts:

services.AddDbContext<ConfigDbContext>(options => options.UseSqlServer(Configuration["Data:ConfigSQLServer:ConnectionString"].ToString(), t => t.MigrationsAssembly("App.Web")));

services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration["Data:AppSQLServer:ConnectionString"].ToString(), t => t.MigrationsAssembly("App.Web")));

当我尝试运行时

dotnet ef migrations add Initial -c AppDbContext

我明白了:

No DbContext named 'AppDbContext' was found

如果我跑:

dotnet ef dbcontext list

结果是:

FullNamespace.ConfigDbContext

找不到第二个DbContext AppDbContext

ConfigDbCOntext:

public class ConfigDbContext : DbContext
{
    public ConfigDbContext()
    {

    }

    public ConfigDbContext(DbContextOptions<ConfigDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

AppDbContext:

public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    public AppDbContext()
    {

    }

    public ConfigDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

2 个答案:

答案 0 :(得分:0)

我有类似的设置,除了我的DbContext和我的IdentityDbCotenxt存在于除Web项目之外的不同程序集中。

ConfigDbContext

// Remove the parameter-less constructor

public class ConfigDbContext : DbContext
{
    public ConfigDbContext(DbContextOptions<ConfigDbContext> options) : base(options)
    { }

    rotected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

AppDbContext

// Remove the parameter-less constructor
// Rename the constructor to AppDbContext, instead of ConfigDbContext

public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

同样在我的IdentityDbContext中,如果您将主键更改为Guid,我必须覆盖AppUserClaimAppUserRoleAppUserLoginAppRoleClaim和{ {1}}也是。

我的IdentityDbContext

示例
AppUserToken

我不认为这是问题,但如果我的建议从一开始就没有解决问题,可能会试一试。

答案 1 :(得分:0)

AppDbContext应为:

public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}