C# DbInitializer失败,必须在根类型上配置密钥

C# DbInitializer失败,必须在根类型上配置密钥,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,我最近将我们的.NET1.1.4代码迁移到.NET2.0.0,看起来一切正常。现在,我尝试通过执行以下操作添加新模型/表: dotnet ef更新 我得到以下异常: DbInitializer失败!无法在“ApplicationUser”上配置密钥,因为它是派生类型。必须在根类型“IdentityUser”上配置密钥 我有一个扩展IdentityUser的ApplicationUser和扩展IdentityRole的ApplicationRole,这是其他一些教程/stackoverflow文章

我最近将我们的.NET1.1.4代码迁移到.NET2.0.0,看起来一切正常。现在,我尝试通过执行以下操作添加新模型/表:

dotnet ef更新

我得到以下异常:

DbInitializer失败!无法在“ApplicationUser”上配置密钥,因为它是派生类型。必须在根类型“IdentityUser”上配置密钥

我有一个扩展IdentityUser的ApplicationUser和扩展IdentityRole的ApplicationRole,这是其他一些教程/stackoverflow文章所说的。

我不确定我还能错过什么。

在ApplicationUser类中,我有:
i收集

它必须是像ApplicationUserRole这样的自定义类型吗?IdentityUserRole和IdentityRole有什么区别?


编辑:
我想这可能是因为在
OnModelCreating
中的context.cs文件中,我有:

builder.Entity<ApplicationUser>()
    .ToTable("AspNetUsers")
    .HasKey(x => x.Id);
builder.Entity()
.ToTable(“AspNetUsers”)
.HasKey(x=>x.Id);
这里的
Id
继承自
IdentityUser


EDIT2:
所以我注释掉了那行,错误就消失了。它转到下一行,这基本上是相同的,但是对于我的应用程序角色,它扩展了IdentityRole。我不太清楚这意味着什么。是否需要覆盖id属性?


EDIT3:

好的,我将builder.Entity中的类型更改为
IdentityUser
IdentityRole
。为什么在startup.cs中我必须在调用
服务时使用
ApplicationUser
ApplicationRole
。additionity

听起来您在项目的某个地方引用了IdentityUser

Ctrl+F“IdentityUser”整个解决方案,然后将对IdentityUser的任何和所有引用更改为ApplicationUser(ApplicationUser类中从其派生的位置除外)


希望这有帮助

我也遇到了这个问题,但你的答案有一半是正确的。我已经注意到,我只有IdentityUser作为我的ApplicationUser类的派生,所以当我看起来更好时,我注意到问题仍然在ApplicationUser类中。如果您从Microsoft文档复制粘贴整个代码,您将被默认导航属性(IdentityUserClaim、IdentityUserLogin、IdentityUserToken和IdentityUserRole)卡住,这些属性应转换为ApplicationUserClaim。。。因为您不能在Identityxxxyyyy类上声明两次主键。