我试图使用ApplicationUser自定义IdentityUser,我按照Microsoft文章中的步骤进行操作,但是当我运行该应用程序时,在方法base.OnModelCreating(modelBuilder);
上遇到此错误:
System.InvalidOperationException HResult = 0x80131509 Message =一个密钥 无法在“ ApplicationUser”上进行配置,因为它是派生的 类型。密钥必须在根类型“ IdentityUser”上配置。如果 您不打算在模型中包含“ IdentityUser”, 确保它不包含在您上下文的DbSet属性中, 在对ModelBuilder的配置调用中引用,或从 模型中包含的类型上的导航属性。
来源= Microsoft.EntityFrameworkCore StackTrace:at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.SetPrimaryKey(IReadOnlyList1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList
1 属性,ConfigurationSource configurationSource) Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList1 clrProperties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder
1.HasKey(Expression1 keyExpression) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext
5。<> c__DisplayClass20_0.b__0(EntityTypeBuilder1 b) at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action
1 buildAction)在 Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext5.OnModelCreating(ModelBuilder builder) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
8.OnModelCreating(ModelBuilder 建筑商) Assistent_de_Estagio.Data.ApplicationDbContext.OnModelCreating(ModelBuilder C:\ Users \ gui \ source \ repos \ Assistente De中的modelBuilder) Estágio\ Estagio \ Data \ ApplicationDbContext.cs:第40行
在 Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder,DbContext上下文) Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder,DbContext上下文) Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext 上下文,IConventionSetBuilder ConventionSetBuilder,IModelValidator 验证者) Microsoft.EntityFrameworkCore.Infrastructure.ModelSource。<> c__DisplayClass5_0.b__1() 在System.Lazy`1.ViaFactory(LazyThreadSafetyMode模式)下
上下文代码:
public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public virtual DbSet<Curso> Curso { get; set; }
public virtual DbSet<Documento> Documento { get; set; }
public virtual DbSet<OpcaoRequisito> OpcaoRequisito { get; set; }
public virtual DbSet<ProgressoDoUsuario> ProgressoDoUsuario { get; set; }
public virtual DbSet<Requisito> Requisito { get; set; }
public virtual DbSet<RequisitoDeDocumento> RequisitoDeDocumento { get; set; }
public virtual DbSet<RequisitoDeUsuario> RequisitoDeUsuario { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
答案 0 :(得分:0)
您需要将使用IdentityUser的代码中的所有位置替换为ApplicationUser。
例如在脚手架的Identity / Pages / Account / Register.cshtml.cs 中,您需要进行以下更改:
private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;
收件人
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
我遇到了同样的问题。在代码中将所有用法IdentityUser替换为ApplicationUser之后,错误消失了。