AspNetUserRole table 在自定义 .NET 后在数据库中创建多个列

AspNetUserRole table creating multiple columns in the database after customizing .NET

我正在使用 EF Core 5 并扩展了 EF Core Identity 来管理用户身份验证。但是当我将我的实体对象迁移到数据库时,它会在数据库中创建额外的列,例如:

AspNetUserRole table 在数据库中创建 4 列 UserId UserId1 RoleId RoleId1 在数据库中

以下是我的扩展 类 给你更多的见解:

ASP.NET 用户实体:

public class AspNetUser : IdentityUser
    {
        public AspNetUser()
        {
            UserRoles = new List<AspNetUserRoles>();
            AspNetUserShippingInfo = new List<AspNetUserShippingInfo>();
            AspNetUserPaymentInfo = new List<AspNetUserPaymentInfo>();

        }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ExternalUserId { get; set; }
    public string ExternalProviderName { get; set; }
    public bool VerifiedEmail { get; set; }

    [ForeignKey("Vendor")]
    public int? VendorId { get; set; }
    public virtual Vendor Vendor { get; set; }


    //TODO: remove from here. doesn't belong here maybe
    [ForeignKey("DeliveryCompany")]
    public int? DeliveryCompanyId { get; set; }
    public virtual DeliveryCompanies DeliveryCompany { get; set; }

    public virtual IList<AspNetUserShippingInfo> AspNetUserShippingInfo { get; set; }
    public virtual IList<AspNetUserPaymentInfo> AspNetUserPaymentInfo { get; set; }
    public virtual IList<AspNetUserRoles> UserRoles { get; set; }

    public int? PreferredZipCode { get; set; }
    public string PreferredLanuage { get; set; }
}

ASP.NET 角色实体:

public class AspNetRoles : IdentityRole<string>
{
    public AspNetRoles()
    {
        UserRoles = new List<AspNetUserRoles>();
    }
    public virtual IList<AspNetUserRoles> UserRoles { get; set; }
}

ASP.NET 用户角色实体:

public class AspNetUserRoles : IdentityUserRole<string>
{
    public AspNetUserRoles() : base()
    {

    }
    public override string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual AspNetUser User { get; set; }

    public override string RoleId { get; set; }
    [ForeignKey("RoleId")]
    public virtual AspNetRoles Role { get; set; }
}

我不确定我做错了什么导致在数据库中生成这些额外的列。任何帮助将不胜感激。谢谢

您可以执行以下步骤来解决您的问题。

1:改变你的AspNetUserRoles

 public class AspNetUserRoles : IdentityUserRole<string>
{
    public AspNetUserRoles() : base()
    {
    }
    public override string UserId { get; set; }
    public virtual AspNetUser User { get; set; }
    public override string RoleId { get; set; }
    public virtual AspNetRoles Role { get; set; }
}

2:在您的上下文中:

public class ApplicationDbContext : IdentityDbContext<AspNetUser, AspNetRoles, string,
    IdentityUserClaim<string>, AspNetUserRoles, IdentityUserLogin<string>,
    IdentityRoleClaim<string>, IdentityUserToken<string>>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //add this code,do not set a composite primary key.

        modelBuilder.Entity<AspNetUser>(b =>
        {
            b.HasMany(e => e.UserRoles)
                .WithOne(e => e.User)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });

        modelBuilder.Entity<AspNetRoles>(b =>
        {
            b.HasMany(e => e.UserRoles)
                .WithOne(e => e.Role)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();
        });
    }

3:迁移和更新数据库。

如果您不包含此代码,那么您的代码可以正常运行您的 AspNetUserRoles Class 看起来像这样

public class AspNetUserRoles : IdentityUserRole<string>
{
    public AspNetUserRoles() : base()
    {
    }    
    [Key]
    public virtual AspNetUser User { get; set; }   
    [Key]
    public virtual AspNetRoles Role { get; set; }
}

你也可以这样做

public class AspNetUser : IdentityUser
    {
    public AspNetUser()
    {
     this.Role=new HashSet<AspNetRoles>();
    }
   public virtual ICollection<AspNetRoles> Role { get; set; }
}
public class AspNetRoles : IdentityRole<string>
{
    public AspNetRoles()
    {
        this.User=new Hashest<AspNetUser>();
    }
    public virtual ICollection<AspNetUser> User { get; set; }
}

如果您选择第二个选项,那么您不需要为多对多关系添加额外的 Class AspNetUserRoles 试试这个最后的代码它在我的电脑上运行良好

 public class AspNetRoles
    {
        public string RoleId { get; set; }
        public string USerId { get; set; }

    }