简体   繁体   English

如何在 ASP.NET Core 5 中的 IdentityUser 和 IdentityRole 之间使用隐式多对多

[英]How to use Implicit Many to Many between IdentityUser and IdentityRole in ASP.NET Core 5

I have these 2 entities.我有这两个实体。

public class ApplicationUser : IdentityUser<int>
    {
        public int DepartmentId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NationalCode { get; set; }
        public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.Now;
        public DateTimeOffset DateModified { get; set; } = DateTimeOffset.Now;
        public CorporateTitle CorporateTitle { get; set; }

        public Department Department { get; set; }
        public ICollection<ApplicationRole> Roles { get; set; } //* instead of IdetityUserRole

  public class ApplicationRole : IdentityRole<int>
    {
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public ICollection<ApplicationUser> Users { get; set; } //* instead of IdetityUserRole

    }

I want to Customize these 2 entities for example overriding navigation properties.我想自定义这两个实体,例如覆盖导航属性。 I don't want to use IdentityUserRole Class for many to many relationship between these classes.我不想将 IdentityUserRole Class 用于这些类之间的多对多关系。

How should I write a configuration for this type of implicit Many to Many in Identity Core?我应该如何在 Identity Core 中为这种类型的隐式多对多编写配置?

 builder.Entity<ApplicationUser>()
                .HasMany(appUser => appUser.Roles)
                .WithMany(role => role.Users)
                .UsingEntity<>(); // here I don't know how to config this relationship.

this is the configuration need for this scenario.这是此场景的配置需求。 (Skip Navigation EF Core 5) (跳过导航 EF Core 5)

 builder.Entity<ApplicationUser>()
                        .HasMany(u => u.Roles)
                        .WithMany(r => r.Users)
                        .UsingEntity<IdentityUserRole<int>>
                        (au => au.HasOne<ApplicationRole>().WithMany(role => role.UserRoles).HasForeignKey(u=> u.RoleId),
                        au => au.HasOne<ApplicationUser>().WithMany(user => user.UserRoles).HasForeignKey(r=> r.UserId));

for complete source code: https://github.com/ArminShoeibi/ImplicitManyToManyIdentityCore完整的源代码: https://github.com/ArminShoeibi/ImplicitManyToManyIdentityCore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM