IdentityDbContext and generic DbContextOptions parameter

Created on 13 Sep 2016  路  2Comments  路  Source: aspnet/Identity

When using IdentityDbContext alongside another DbContext, an InvalidOperationException is thrown. This is due to the usage of the generic DbContextOptions in the IdentityDbContext constructor (not an issue in the VS templates due to the ApplicationDbContext constructor).

The DbContextOptions passed to the Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext constructor must be a DbContextOptions<Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext>. When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather than a non-generic DbContextOptions parameter.

Most helpful comment

Currently I'm using the following workaround, similar to what you suggested:

public class ApplicationDbContext : IdentityDbContext {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

Seems a bit odd to be made to do this when you want to use more than one DbContext in a project. Shouldn't this be the default behavior of the library?

All 2 comments

Could you try to use a Generic parameter like that?

public IdentityDbContext (DbContextOptions<IdentityDbContext > options)
   : base(options)
{
}

Currently I'm using the following workaround, similar to what you suggested:

public class ApplicationDbContext : IdentityDbContext {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

Seems a bit odd to be made to do this when you want to use more than one DbContext in a project. Shouldn't this be the default behavior of the library?

Was this page helpful?
0 / 5 - 0 ratings