2

我有 2 个 DbContext:

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

找不到第二个 DbContextAppDbContext

配置数据库上下文:

public class ConfigDbContext : DbContext
{
    public ConfigDbContext()
    {

    }

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

    }

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

应用数据库上下文:

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

2 回答 2

1

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

配置数据库上下文

// 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我还必须覆盖AppUserClaimAppUserRoleAppUserLogin和。AppRoleClaimAppUserToken

我的 IdentityDbContext 示例

public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, Guid, AppUserClaim, AppUserRole,
    AppUserLogin, AppRoleClaim, AppUserToken>
{
    public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<AppUser>().ToTable("User");
        builder.Entity<AppRole>().ToTable("Role");
        builder.Entity<AppUserRole>().ToTable("UserRole");

        builder.Entity<AppUserClaim>().ToTable("UserClaim");
        builder.Entity<AppRoleClaim>().ToTable("RoleClaim");

        builder.Entity<AppUserLogin>().ToTable("UserLogin");
        builder.Entity<AppUserToken>().ToTable("UserToken");
    }
}

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

于 2017-10-12T21:00:14.797 回答
0

AppDbContext 应该是:</p>

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

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
于 2017-10-13T03:20:00.297 回答