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

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

问题描述

我在Linux上使用dotnet core 1.1,并且每当我在startup.cs-> configure中运行以下行时,当我想从常规dbContext中拆分identityContext时遇到问题.

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 party类,我什至没有碰过.我有以下简单设置:

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


        }
    }
}

和applicationUser:

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

}

在我的创业公司中,我通过以下方式配置身份框架:

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

configureServices:

configureServices:

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内部执行以下操作,以通过以下方式为IdentityUserLogin提供主键:

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是否应为其primaryKey.我可以得到其他错误,而不是这个错误,

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.

推荐答案

标识表的键被映射到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&lt; string&gt;"需要定义一个主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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