首页 文章

实体类型'IdentityUserLogin<string>'需要定义主键[重复]

提问于
浏览
10

这个问题在这里已有答案:

我在linux上使用dotnet core 1.1,当我想从我的常规dbContext中分离identityContext时,我遇到问题,每当我在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);


        }
    }
}

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

配置:

app.UseIdentity();

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

如果这有帮助 .

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

modelBuilder.Entity<IdentityUserLogin<int>>()
            .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,但这给出了完全相同的错误 .

3 回答

  • 3

    身份表的键在 IdentityDbContextOnModelCreating 方法中映射,如果未调用此方法,您将最终得到您所获得的错误 .

    你需要做的就是打电话 .

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

    原始答案(只是为了以防复制,仅供其他人参考)here

  • 42

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

    主要是出了什么问题,是因为我在尝试迁移_2680374时遇到此错误但实际上错误是基于我在应用程序中的其他dbContext引发的,当时我试图在那个上运行迁移 . 我仍然有点不知道为什么其他DbContext选择了我没有在任何地方提到的这些Identity实体,我认为IdentityDbContext是错误的,我只是在OnModelCreating的上下文中添加了以下内容抛出错误:

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

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

  • 4

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

    实体类型'IdentityUserLogin'需要定义主键 .

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

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

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

    在确定并指定应将哪个属性指定为该实体的主键后,错误将消失 .

相关问题