2

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

2 Answers 2

1

I have similar setups, except that my DbContext and my IdentityDbCotenxt live in different assemblies other than the web project.

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

Also in my IdentityDbContext, if you change the primary key to Guid, I have to override AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim and AppUserToken as well.

My example of 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");
    }
}

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.