I have 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")));
when I try to run
dotnet ef migrations add Initial -c AppDbContext
I get:
No DbContext named 'AppDbContext' was found
If I run:
dotnet ef dbcontext list
the result is:
FullNamespace.ConfigDbContext
It doesn't seam to find the second 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);
}
}
I have similar setups, except that my DbContext and my IdentityDbCotenxt live in different assemblies other than the web project.
// 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);
}
}
// 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);
}
}
Also in my IdentityDbContext, if you change the primary key to Guid
, I have to override AppUserClaim
, AppUserRole
, AppUserLogin
, AppRoleClaim
and AppUserToken
as well.
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");
}
}
I don't think this is the issue but if my suggestions from the beginning don't fix the issue, might give this a trial.
AppDbContext should be:
public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.