实体类型 'IdentityUserLogin<string>'需要定义一个主键 [英] The entity type &#39;IdentityUserLogin&lt;string&gt;&#39; requires a primary key to be defined

查看:21
本文介绍了实体类型 'IdentityUserLogin<string>'需要定义一个主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am using dotnet core 1.1 on linux, and i am having issues when i want to split up the identityContext from my regular dbContext, whenever i run the following line in my 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'需要定义一个主键

So this line throws the exception: The entity type 'IdentityUserLogin' requires a primary key to be defined

我只是不明白这一点,为什么我的工作是给 IdentityUserLogin 一个主键??,这是一个 3rd 方类,我什至没有接触过它.我有以下简单的设置:

I simply don't understand this, why is it my job to give the IdentityUserLogin a primary key??, it is a 3rd party class and i haven't even touched it. I have the following simple setup:

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; }
    }

}

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

In my startup i am configuring the identity framework the following way:

配置服务:

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

还有

配置:

 app.UseIdentity();

我的项目在以下位置开源:我的 github 存储库

My project is opensourced at : my github repo

如果有帮助的话.

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

I have tried a lot of things. The two most promising was deriving all of the classes used in this generic show, and passing them in explicitly, tried to change all of their keys to ints etc. And that gave the exact same error just with int instead of string. The other way i tried was to do the following inside of OnModelCreating to give IdentityUserLogin a primary key by e.g :

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

如您所见,当我将 UserId 作为整数时,这又回来了,但我什至不确定 UserId 是否应该是其主键.我可以得到其他错误而不是这个错误,说

As you can see, this was back when i had UserId as a integer, but i am not even sure if the UserId should be its primaryKey. I can get other errors instead of this one, that says

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

IdentityUserLogin is part of a hierarchy, and it has no discriminator values

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

But if I had discriminator values it eventually just goes back to this error. The weirdest part i think is that i have the EXACT same implementation as the UnicornStore github example, that uses a bit of the identity framework as well .... So i really need your help guys. Can reproduce this error by downloading the project, copying the default.appsettings.json into appsettings.json, put in a valid connectionstring, dotnet restore, run with dotnet run --environment Development.

我什至尝试更改实现以使用 MSSQL 数据库而不是 MySQL,但这给出了完全相同的错误.

I even tried to change out the implementation to use a MSSQL database instead of MySQL, but that gave the exact same error.

推荐答案

Identity 表的keys 映射到IdentityDbContextOnModelCreating 方法中,如果没有调用这个方法,你最终会得到你得到的错误.

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

这篇关于实体类型 'IdentityUserLogin<string>'需要定义一个主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆