Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to keep my applicaition and identity handling on their own seperate contexts, but I am running up agaisnt the "Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context." error message in doing so.

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 Stackoverflow post tells me this is not allowed.

What I have tried:

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.


Here is the post that has me believe two contexts are not possible. Mean while this post throws cold water over my intention to have my own user class.

Someone please say it ain't so, and guide me to a solution.
Posted
Updated 13-Nov-19 19:28pm
v2

1 solution

Mistake in my startup.cs, spotted by our colleagues in stackoverflow.

services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
 .AddEntityFrameworkStores<AppIdentityDbContext>()
 .AddDefaultTokenProviders();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900