Search code examples
c#entity-framework-coreasp.net-identityasp.net-core-webapi

How do I resolve Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context


I am trying to keep my application and identity handling on their own separate contexts, but I am running up against the error message

Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context.

From my online research it appears that I need to have ApplicationDBContext inherit from IdentityDbContext rater than DBContext, but as stated, I want to have to have identity management in its own context. At least one Stack Overflow post tells me this is not allowed, has any of the community managed to keep identity in a its own context?

This is my Identity context:

public class AppIdentityDbContext : IdentityDbContext<CUserMasterLocal, CRoleMasterLocal, string>
{
    public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
    : base(options) { }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // Customizations must go after base.OnModelCreating(builder)

        var x = builder.Entity<CUserMasterLocal>(config =>
        {
            // Set the default table name of AspNetUsers
            config.ToTable("AspNetUsers");
        });


        builder.Entity<CRoleMasterLocal>(config =>
        {
            config.ToTable("RoleMaster");
        });
    }
    public DbSet<CUserMasterLocal> UserMaster { get; set; }
    public DbSet<CRoleMasterLocal> RoleMaster { get; set; }
}

In startup.cs I have

var identityConnectionString = this.Configuration["Data:VT_LocalIdentityData:ConnectionString"];
services.AddDbContext<AppIdentityDbContext>(options =>
    options.UseSqlServer(identityConnectionString));
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

Within my controller, I have isolated the identity activity via a 'block'

using (var localIdContext = new AppIdentityDbContext(optionsBuilder.Options))
{
    CUserMasterLocal userMaster = CreateUserMasterEntry(person, 
        userDetails.GetUsername());

    IdentityResult result = this.userManager.CreateAsync(userMaster, 
        GeneratePassword(12)).Result;
}

For the avoidance of doubt, CUserMasterLocal is not referenced in the class that sets up ApplicationDBContext, and I would like to keep it that way.


Solution

  • When you're registering your Identity services did you mean to have ApplicationDbContext as your EntityFrameworkStores?

    Try replacing

    services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
                .AddEntityFrameworkStores<ApplicationDbContext>() // <--- this doesn't seem right.
                .AddDefaultTokenProviders();
    

    with

    services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
                .AddEntityFrameworkStores<AppIdentityDbContext>() // <--- this is changed
                .AddDefaultTokenProviders();