我如何解决 Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context
How do I resolve Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context
我试图将我的应用程序和身份处理保留在它们自己单独的上下文中,但我运行反对错误消息
Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context.
根据我的在线研究,我似乎需要 ApplicationDBContext
从 IdentityDbContext
继承而不是 DBContext
,但如前所述,我想必须在其中进行身份管理自己的语境。至少有一个 Stack Overflow post 告诉我这是不允许的,社区中有没有人设法在自己的上下文中保持身份?
这是我的身份上下文:
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; }
}
在startup.cs我有
var identityConnectionString = this.Configuration["Data:VT_LocalIdentityData:ConnectionString"];
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(identityConnectionString));
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
在我的控制器中,我通过 'block'
隔离了身份 activity
using (var localIdContext = new AppIdentityDbContext(optionsBuilder.Options))
{
CUserMasterLocal userMaster = CreateUserMasterEntry(person,
userDetails.GetUsername());
IdentityResult result = this.userManager.CreateAsync(userMaster,
GeneratePassword(12)).Result;
}
为避免疑义,CUserMasterLocal
未在设置 ApplicationDBContext
的 class 中引用,我希望保持这种状态。
当您注册您的身份服务时,您是否打算将 ApplicationDbContext
作为您的 EntityFrameworkStores?
尝试替换
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>() // <--- this doesn't seem right.
.AddDefaultTokenProviders();
和
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<AppIdentityDbContext>() // <--- this is changed
.AddDefaultTokenProviders();
我试图将我的应用程序和身份处理保留在它们自己单独的上下文中,但我运行反对错误消息
Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context.
根据我的在线研究,我似乎需要 ApplicationDBContext
从 IdentityDbContext
继承而不是 DBContext
,但如前所述,我想必须在其中进行身份管理自己的语境。至少有一个 Stack Overflow post 告诉我这是不允许的,社区中有没有人设法在自己的上下文中保持身份?
这是我的身份上下文:
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; }
}
在startup.cs我有
var identityConnectionString = this.Configuration["Data:VT_LocalIdentityData:ConnectionString"];
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(identityConnectionString));
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
在我的控制器中,我通过 'block'
隔离了身份 activityusing (var localIdContext = new AppIdentityDbContext(optionsBuilder.Options))
{
CUserMasterLocal userMaster = CreateUserMasterEntry(person,
userDetails.GetUsername());
IdentityResult result = this.userManager.CreateAsync(userMaster,
GeneratePassword(12)).Result;
}
为避免疑义,CUserMasterLocal
未在设置 ApplicationDBContext
的 class 中引用,我希望保持这种状态。
当您注册您的身份服务时,您是否打算将 ApplicationDbContext
作为您的 EntityFrameworkStores?
尝试替换
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<ApplicationDbContext>() // <--- this doesn't seem right.
.AddDefaultTokenProviders();
和
services.AddIdentity<CUserMasterLocal, CRoleMasterLocal>()
.AddEntityFrameworkStores<AppIdentityDbContext>() // <--- this is changed
.AddDefaultTokenProviders();