实体类型’IdentityUserLogin ‘需要定义主键

我在linux上使用dotnet core 1.1,当我想从我的常规dbContext中分离identityContext时,我遇到问题,每当我在startup.cs中运行以下行时 – > configure:

//... some other services using (var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope()) { serviceScope.ServiceProvider.GetService().Database.Migrate(); //running other database.migrations here + seeding data. But it is the line above that causes problems 

因此,该行抛出exception:实体类型’IdentityUserLogin’需要定义主键

我根本不明白这一点,为什么我的工作是给IdentityUserLogin一个主键?它是一个第三方类,我甚至没有触及它。 我有以下简单的设置:

 namespace EsportshubApi.Models { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public ApplicationDbContext() { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } } 

和applicationUser:

 namespace EsportshubApi.Models.Entities { public class ApplicationUser : IdentityUser { public ApplicationUser() { } public static ApplicationUserBuilder Builder() { return new ApplicationUserBuilder(new ApplicationUser()); } public int AccountId { get; set; } public Guid AccountGuid { get; set; } public string Salt { get; set; } public bool Verified { get; set; } public string Checksum { get; set; } public string Password { get; set; } public DateTime Created { get; set; } public DateTime Updated { get; set; } } } 

在我的启动中,我按以下方式配置身份框架:

configureServices:

 services.AddEntityFrameworkSqlServer().AddMySQL().AddDbContext(options => options.UseMySQL(config["ConnectionStrings:DefaultConnection"])); 

配置:

  app.UseIdentity(); 

我的项目是开源的: 我的github repo

如果这有帮助。

我尝试了很多东西。 最有希望的两个是派生出这个通用节目中使用的所有类,并明确地传递它们,试图将它们的所有键更改为整数等。而这只是用int而不是字符串给出完全相同的错误。 我尝试的另一种方法是在OnModelCreating执行以下操作,以通过例如:IdentityUserLogin为主键提供:

  modelBuilder.Entity<IdentityUserLogin>() .Property(login => login.UserId) .ForMySQLHasColumnType("PK") .UseSqlServerIdentityColumn() .UseMySQLAutoIncrementColumn("AI"); 

正如你所看到的,当我将UserId作为整数时,这又回来了,但我甚至不确定UserId是否应该是它的primaryKey。 我说可以得到其他错误,而不是这个错误

IdentityUserLogin是层次结构的一部分,它没有鉴别器值

但如果我有鉴别器值,它最终会回到这个错误。 我认为最奇怪的部分是我有与UnicornStore github示例完全相同的实现,它也使用了一些身份框架….所以我真的需要你的帮助。 可以通过下载项目重现此错误,将default.appsettings.json复制到appsettings.json ,放入有效的连接appsettings.jsondotnet restore ,运行dotnet run --environment Development

我甚至试图改变实现以使用MSSQL数据库而不是MySQL,但这给出了完全相同的错误。

Identity表的键在IdentityDbContext的OnModelCreating方法中映射,如果未调用此方法,则最终会得到错误。

你需要做的就是打电话。

 protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } 

这里是原始答案(只是复制以供其他人参考)

好的。 所以我会尝试回答我自己的问题,因为我确实已经过了它。 它仍然可以跟随OP中的github链接,并看到我收到错误的项目。

主要是出了什么问题,是因为我在尝试迁移ApplicationDbContext : IDentityContext遇到此错误ApplicationDbContext : IDentityContext但实际上错误是基于我在应用程序中的其他dbContext引发的,当时我试图在那个上运行迁移。 我仍然有点不知道为什么其他DbContext选择了我没有在任何地方提到的这些Identity实体,我认为dbcontext似乎对OnModelCreating方法中未提及的实体有所了解是奇怪的。 无论如何 – 当我发现IdentityDbContext不是出现问题时,我只是在OnModelCreating中添加了以下内容:

  protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Ignore >(); modelBuilder.Ignore >(); modelBuilder.Ignore>(); modelBuilder.Ignore>(); modelBuilder.Ignore>(); modelBuilder.Ignore(); 

所以…我仍然想知道为什么我的上下文在没有与它们有任何关系的情况下接受这些实体,我非常担心每次添加上下文时我必须排除模型而不是包含它们。

我有一个与你最初的问题有关的类似问题

实体类型’IdentityUserLogin’需要定义主键。

虽然我认为忽略实体的解决方案似乎有效,但我只是想为那些实际想要为每个实体分配主键的人提供另一种解决方案。 问题在于,无论何时创建实体, DbContext都希望跟踪每个实体的主键 – 因此,您必须分配它。

请参阅下面的示例,其中ProjectTarget是我的实体,并且我已经为每个属性分配了一个我想用作每个实体的主键的属性。

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity().HasKey(m => m.ProjectPath); builder.Entity().HasKey(m => m.Guid); base.OnModelCreating(builder); } 

在您识别并指定应将哪个属性指定为该实体的主键后,错误将消失。