实体类型“IdentityUserRole<string>”需要定义主键

The entity type 'IdentityUserRole<string>' requires a primary key to be defined

提问人:Sascha Heimann 提问时间:9/23/2022 更新时间:9/20/2023 访问量:640

问:

我有以下代码:

using MyBlazorApp.Server.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MyBlazorApp.Server.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }        

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

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.firstName)
                .HasMaxLength(250);

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.lastName)
                .HasMaxLength(250);

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.isLdapLogin);

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.isMFAforce);

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.apiKey);

            modelBuilder.Entity<ApplicationUser>()
                .Property(e => e.IsEnabled);

            //Seeding a  'Administrator' role to AspNetRoles table
            var arId = "dffc6dd5-b145-41e9-a861-c87ff673e9ca";
            modelBuilder.Entity<IdentityRole>().HasData(
                new IdentityRole
                {
                    Id = arId,
                    Name = "Admins",
                    NormalizedName = "ADMINS".ToUpper(),
                    ConcurrencyStamp = arId
                }
            ); ;
            var urId = "f8a527ac-d7f6-4d9d-aca6-46b2261b042b";
            modelBuilder.Entity<IdentityRole>().HasData(
                new IdentityRole
                {
                    Id = urId,
                    Name = "Users",
                    NormalizedName = "USERS".ToUpper(),
                    ConcurrencyStamp = urId
                }
            ); ;

            //a hasher to hash the password before seeding the user to the db
            var hasher = new PasswordHasher<IdentityUser>();

            //Seeding the Admin User to AspNetUsers table
            modelBuilder.Entity<ApplicationUser>().HasData(
                new ApplicationUser
                {
                    Id = new Guid("6fbfb682-568c-4f5b-a298-85937ca4f7f3"), // primary key
                    UserName = "super.admin",
                    NormalizedUserName = "SUPER.ADMIN",
                    PasswordHash = hasher.HashPassword(null, "7ugVUczrm7"),
                    firstName = "Super",
                    lastName = "Admin",
                    Email = "[email protected]",
                    NormalizedEmail = "[email protected]",
                    EmailConfirmed = true,
                    isMFAforce = 0,
                    isLdapLogin = 0
                }
            );

            List<IdentityUserRole<string>> UserRoles = new List<IdentityUserRole<string>>();
            UserRoles.Add(new IdentityUserRole<string>
            {
                RoleId = "dffc6dd5-b145-41e9-a861-c87ff673e9ca",
                UserId = "6fbfb682-568c-4f5b-a298-85937ca4f7f3"
            });
            modelBuilder.Entity<IdentityUserRole<string>>().HasData(UserRoles);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

    }
}

当我点击命令时,出现以下错误:实体类型“IdentityUserRole”需要定义主键。dotnet ef migrations add IntitialMigration

我已经找到了我必须使用base的最受欢迎的解决方案。OnModelCreating(modelBuilder);。但正如你在我的代码中看到的那样,我已经使用了它。

我希望有人知道为什么会抛出这个错误。

C# entity-framework-core blazor asp.net-identity

评论


答:

1赞 DABIN 10/26/2022 #1

我猜您可能已经解决了这个问题,但我遇到了同样的问题,并通过在实体上指定 HasNoKey() 解决了它。

modelBuilder.Entity<IdentityUserRole<string>>(entity => 
{
    entity.HasNoKey();
    entity.HasData(UserRoles);
}
1赞 Daniel Hall 9/20/2023 #2

我也遇到了这个问题。我想默认保留表附带的主键。不确定您是先做代码还是先做数据库的方法,但我发现后来会弹出这个错误。简单的解决方案是注释掉 DbSets 和 OnModelCreating,运行迁移,运行数据库更新,然后取消注释所有内容。希望这对未来的人有所帮助。

评论

0赞 jesse 9/25/2023
您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。