我试图将我的应用程序和身份处理保留在各自独立的上下文中,但我遇到了错误消息
无法为"CUserMasterLocal"创建DbSet,因为此类型未包含在上下文的模型中。
根据我的在线研究,我似乎需要让ApplicationDBContext
从IdentityDbContext
继承,而不是从DBContext
继承,但如上所述,我希望在自己的上下文中进行身份管理。至少有一篇Stack Overflow帖子告诉我这是不允许的,社区中的任何人都能在自己的环境中保持身份吗?
这是我的身份上下文:
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; }
}
在启动.cs中,我有
var identityConnectionString = this.Configuration["Data:VT_LocalIdentityData:ConnectionString"];
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(identityConnectionString));
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
在我的控制器内,我通过"阻止"隔离了身份活动
using (var localIdContext = new AppIdentityDbContext(optionsBuilder.Options))
{
CUserMasterLocal userMaster = CreateUserMasterEntry(person,
userDetails.GetUsername());
IdentityResult result = this.userManager.CreateAsync(userMaster,
GeneratePassword(12)).Result;
}
为避免疑义,CUserMasterLocal
在设置ApplicationDBContext
的类中没有被引用,我希望保持这种方式。
当您注册您的身份服务时,您是否打算将ApplicationDbContext
作为您的EntityFrameworkStores?
尝试更换
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>() // <--- this doesn't seem right.
.AddDefaultTokenProviders();
带有
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<AppIdentityDbContext>() // <--- this is changed
.AddDefaultTokenProviders();