C# 实体类型';IdentityUserLogin<;字符串>';需要定义主键

C# 实体类型';IdentityUserLogin<;字符串>';需要定义主键,c#,entity-framework,asp.net-core,asp.net-identity,C#,Entity Framework,Asp.net Core,Asp.net Identity,我在linux上使用的是dotnet core 1.1,每当我在startup.cs-->configure中运行以下行时,如果我想从常规dbContext中拆分identityContext,就会出现问题: //... some other services using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) {

我在linux上使用的是dotnet core 1.1,每当我在startup.cs-->configure中运行以下行时,如果我想从常规dbContext中拆分identityContext,就会出现问题:

//... 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
在我的启动中,我以以下方式配置identity framework:

配置服务:

services.AddEntityFrameworkSqlServer().AddMySQL().AddDbContext<ApplicationDbContext>(options =>
                    options.UseMySQL(config["ConnectionStrings:DefaultConnection"]));
我的项目开源于:

如果有帮助的话

我试过很多东西。最有希望的两个方法是派生这个泛型演示中使用的所有类,并显式地传递它们,尝试将它们的所有键更改为int等。这只会产生与
int
完全相同的错误,而不是字符串。我尝试的另一种方法是在
OnModelCreating
中执行以下操作,通过以下方式为IdentityUserLogin提供主键:

 modelBuilder.Entity<IdentityUserLogin<int>>()
            .Property(login => login.UserId)
            .ForMySQLHasColumnType("PK")
            .UseSqlServerIdentityColumn()
            .UseMySQLAutoIncrementColumn("AI");
modelBuilder.Entity()
.Property(login=>login.UserId)
.ForMySQLHasColumnType(“PK”)
.UseSqlServerIdentityColumn()
.UseMySQLAutoIncrementColumn(“AI”);
如您所见,这是在我将UserId作为整数的时候,但我甚至不确定UserId是否应该是它的primaryKey。我可以得到其他的错误,而不是这个,就是说

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

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


我甚至尝试将实现更改为使用MSSQL数据库而不是MySQL,但这会产生完全相同的错误

好的。因此,我将尝试回答我自己的问题,因为我确实克服了它。仍然可以在OP中跟踪github链接,并查看错误所在的项目

主要错误在于,我认为我在尝试迁移
ApplicationDbContext:IDentityContext
时遇到了此错误,但实际上,当我尝试在该应用程序上运行迁移时,该错误是基于应用程序中的其他dbContext引发的。我仍然有点不知道为什么其他DbContext选择了这些我在任何地方都没有提到的标识实体,我认为DbContext似乎知道一些OnModelCreating方法中没有提到的实体的信息是很奇怪的。无论如何-当我发现不是IdentityDbContext出了问题时,我只是在引发错误的上下文的onmodel创建中添加了以下内容:

 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>();
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
Ignore();
Ignore();
Ignore();
Ignore();
Ignore();
Ignore();

所以…我仍然想知道为什么我的上下文在与这些实体无关的情况下拾取这些实体,我非常担心每次添加上下文时,我都必须排除模型,而不是将它们包括在内。

我遇到了一个类似的问题,与您最初的问题有关,在哪里

实体类型“IdentityUserLogin”需要主键 被定义

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

请参见下面的示例,其中
Project
Target
是我的实体,我已为每个实体分配了一个属性,我想将其用作每个实体的主键

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Project>().HasKey(m => m.ProjectPath);
    builder.Entity<Target>().HasKey(m => m.Guid);
    base.OnModelCreating(builder);
} 
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Entity().HasKey(m=>m.ProjectPath);
builder.Entity().HasKey(m=>m.Guid);
基于模型创建(生成器);
} 

在您确定并分配了哪个属性应被指定为该实体的主键后,错误将消失。

标识表的键映射到
OnModelCreating
方法
IdentityDbContext
中,如果未调用此方法,您将最终得到您得到的错误

你只需要打个电话

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

原始答案(仅复制供他人参考,以防万一)

这不是因为您正在扩展IdentityDbContext,它在层次结构上包含您现在忽略的数据库集吗?我现在记不清了,但我确实认为是这样的。上下文确实以某种方式引用了它们。如果IdentityDatabaseContext中的某个实体存在于其他数据库上下文中,EF将从这两个数据库中创建所有实体主数据库上下文中的数据库上下文!。我在未调用数据库的情况下覆盖了它,谢谢!好的,所以我接受这个答案,而不是我自己的答案,即使在我的情况下,我已经有了这一行,但它没有修复它。鉴于我解释的错误,这显然像是修复了很多人的错误。为meI保留这一天有一个类似的情况,t问题是我没有从没有添加到OnModelCreating的配置文件中删除这些文件。一旦我添加了它们,一切都解决了。-Chdid刚刚在我添加base时进行了编辑。OnModelCreating(modelBuilder);它开始抛出AspnetRoles的sql异常不存在。因此我必须添加迁移并更新数据库以使其工作。
 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>();
protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Project>().HasKey(m => m.ProjectPath);
    builder.Entity<Target>().HasKey(m => m.Guid);
    base.OnModelCreating(builder);
} 
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
}