实体类型‘ IdentityUserLogin < string >’需要定义一个主键

我在 linux 上使用 dotnet core 1.1,当我想从常规 dbContext 中分离 Identity Context 时,我遇到了一些问题,无论何时我在 startup.cs 中运行以下代码行—— > configure:

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

因此,这一行引发了异常: 实体类型“ IdentityUserLogin”需要定义一个主键

我就是不明白,为什么我的工作就是给 IdentityUserLogin 一个主键?这是第三方课程,我还没碰过呢。我有以下简单的设置:

namespace EsportshubApi.Models
{
public class ApplicationDbContext :  IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}


public ApplicationDbContext()
{


}


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




}
}
}

以及应用程序用户:

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<ApplicationDbContext>(options =>
options.UseMySQL(config["ConnectionStrings:DefaultConnection"]));

还有

配置:

 app.UseIdentity();

我的项目是开源的: 我的 Github 回购

如果有帮助的话。

我试过很多方法。最有前途的两个方法是派生这个通用显示中使用的所有类,并显式地传递它们,试图将它们的所有键更改为 int 等等。这就给出了 int而不是字符串的完全相同的错误。我尝试的另一种方法是在 OnModelCreating内部执行以下操作,例如:

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

正如你所看到的,这是回到了我把 UserId 作为一个整数的时候,但是我甚至不确定 UserId 是否应该是它的主键。我可以得到其他的错误,而不是这个

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

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

我甚至尝试将实现改为使用 MSSQL 数据库而不是 MySQL,但是这也产生了同样的错误。

78565 次浏览

Okay. So i will try to answer my own question, because i did get past it. Its still possible to follow the github link in the OP, and see the project i got the error in.

Mainly what was wrong, was that i thouth i got this error trying to migrate the ApplicationDbContext : IDentityContext but actually the error was thrown based on my other dbContext in the application, when i tried to run the migration on that one. I am still a little unaware as to why the other DbContext picked up on these Identity entities which i had not referred to anywhere, i think it's odd that a dbcontext seems to know something about entities not mentioned in the OnModelCreating method. Anyway - when i found out that it wasn't the IdentityDbContext that something was wrong with, i simply added the following in the OnModelCreating of the context that threw the error:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore <IdentityUserLogin<string>>();
modelBuilder.Ignore <IdentityUserRole<string>>();
modelBuilder.Ignore<IdentityUserClaim<string>>();
modelBuilder.Ignore<IdentityUserToken<string>>();
modelBuilder.Ignore<IdentityUser<string>>();
modelBuilder.Ignore<ApplicationUser>();

So ... i am still wondering why my context is picking up on these entities without having anything to do with them, and i am pretty worried that each time i add a context i have to exclude models in contrary to including them.

I had a similar issue relating to your initial problem where

The entity type 'IdentityUserLogin' requires a primary key to be defined.

And while I think your solution to ignore entities seems to work, I just wanted to provide another solution for those who actually do want to assign a primary key to each entity. The problem is that whenever you create an entity, the DbContext will want keep track of the primary key for each - thus, you'd have to assign it.

See my example below, where Project and Target are my entities, and I've assigned it each a property I would like to use as the primary key for each entity.

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

After you've identified and assigned which property should be designated as the primary key for that entity, the error will go away.

keys of Identity tables are mapped in OnModelCreating method of IdentityDbContext and if this method is not called, you will end up getting the error that you got.

All you need to do is call.

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

The original answer (just copied for other's reference just in case) here