gpt4 book ai didi

c# - 我如何解决 Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:50 25 4
gpt4 key购买 nike

我试图将我的应用程序和身份处理保留在它们自己单独的上下文中,但我遇到了错误消息

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

从我的在线研究看来,我需要让 ApplicationDBContextIdentityDbContext 继承,而不是 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; }
}

在 startup.cs 我有

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

在我的 Controller 中,我通过“ block ”隔离了身份事件

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();

关于c# - 我如何解决 Cannot create a DbSet for 'CUserMasterLocal' because this type is not included in the model for the context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58843909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com