2

我有2個DbContexts:ASP.NET核心多的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 

它不縫找到第二的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); 
    } 
} 

回答

0

也許你缺少UserSql選項? 喜歡:

services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

也 - 你能分享你的AppDbContext實現?

+0

我做一個編輯對我的問題 – user2818430

+0

廢話...如果你切換周圍的秩序,它只能找到的第一個? – JanivZ

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,我要重寫AppUserClaimAppUserRoleAppUserLoginAppRoleClaimAppUserToken爲好。

我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"); 
    } 
} 

我不認爲這是問題,但如果從一開始我的建議不解決這個問題,可能給這一審判的例子。

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