Using Identity with multiple DbContext

Viewed 86

I want to used Identity Framework which is optional when creating a net Core web project. And use another database.

But I get an error:

The DbContextOptions passed to the ApplicationDbContext constructor must be a DbContextOptions. When registering multiple DbContext types, ensure that the constructor for each context type has a DbContextOptions parameter rather than a non-generic DbContextOptions parameter.

Here is my configuration:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options. UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

builder.Services.AddAuthentication()
    .AddIdentityServerJwt();

// other DbContext
var connectionStringSchool = builder.Configuration.GetConnectionString("SchoolDb")??
    throw new InvalidOperationException("Connection string 'SchoolDBSQL' not found.");
builder.Services.AddDbContext<SchoolContext>(options =>
    options. UseSqlServer(connectionStringSchool));
1 Answers

Modified the ApplicationDbContext constructor.

public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
    {
         public ApplicationDbContext(
             DbContextOptions<ApplicationDbContext> options,
             IOptions<OperationalStoreOptions> operationalStoreOptions): base(options, operationalStoreOptions)
         {
         }
     }
Related