[英]Many to many relationships EF Core 5.0.3
I need to replicate discords users model for my own site and the rules are.我需要为我自己的站点复制不和谐用户 model 并且规则是。
A User can be part of multiple clubs Each user within those clubs can have the roles of admin or mod.一个用户可以是多个俱乐部的一部分这些俱乐部中的每个用户都可以具有管理员或管理员的角色。
So Far I have site wide roles working but not within the club entity and I am having trouble linking up my clubs to the users到目前为止,我有网站范围的角色在工作,但不在俱乐部实体内,我无法将我的俱乐部与用户联系起来
This is what I have so far I am using ef core 5.0.3 if that makes a difference.这就是我目前使用的 ef core 5.0.3,如果这有所作为的话。
ApplicaitonUser Model应用用户Model
public class ApplicationUser : IdentityUser
{
public enum UserTypeEnum
{
Guest=0,
User=1,
Author=2,
AddonOwner=3,
GroupOwner=5,
ClubMember=6,
ClubAdmin=7,
SuperAdmin=199,
BandUser=999
}
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? GamerTag { get; set; }
public bool? isOnline { get; set; }
public int? UserType { get; set; }
public Guid? TennantId { get; set; }
public Guid? ClubId { get; set; }
public List<Badges>? Badges { get; set; }
public List<Club>? Clubs { get; set; }
}
Then In my clubs I have然后在我的俱乐部里,我有
public class Club
{
public int Id { get; set; }
public Guid? TeannatId { get; set; }
public Guid? ClubId { get; set; }
public Guid? UserId { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string? Url { get; set; }
public int? ClubLikes { get; set; }
public int? ClubDislikes { get; set; }
public int? MembersCount { get; set; }
public string? Logo { get; set; }
public List<Flight>? Flights { get; set; }
public string? ThumbNail { get; set; }
public DateTimeOffset? GpdrRemoveRequestDate { get; set; }
public bool? isGpdrRemoveRequest { get; set; }
public DateTimeOffset? BannedTime { get; set; }
public TimeSpan? BanPeriod { get; set; }
public bool? isBanned { get; set; }
public bool? isActive { get; set; }
public bool? isDeleted { get; set; }
public DateTime? CreatedDate { get; set; }
public string? CreatedBy { get; set; }
public List<ApplicationUser>? Members { get; set; }
}
I tried this so far到目前为止我试过这个
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Club>().Property(x => x.ClubId).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
modelBuilder.Entity<ApplicationUser>()
.HasOne<Club>()
.WithMany(m => m.Members)
.HasForeignKey(a => a.ClubId)
.HasPrincipalKey(c => c.ClubId);
}
But it only gives me a admin user and not roles within clubs also.但它只给了我一个管理员用户,而不是俱乐部中的角色。 The mapping for club only allows for one club per members when they could be part of multiple clubs its the exact same principle for discord where a channel would be a club and a user could be a mod or a normal user.
俱乐部的映射只允许每个成员一个俱乐部,当他们可能是多个俱乐部的一部分时,它与 discord 的原理完全相同,其中频道是俱乐部,用户可以是模组或普通用户。
Then in my view I am returning.然后在我看来我要回来了。
var club = await _context.Clubs.Include(c =>c.Members).ToListAsync();
return View(club);
Where I would expect to see the many members per club but each member in term could belong to a different club multple times我希望看到每个俱乐部有很多成员,但每个成员在任期内可能属于不同的俱乐部多次
Edit 2 still issues编辑 2 仍然存在问题
I am still getting a zero count on my club users from the below person answer is their something else i must configure我仍然从下面的人那里得到零计数我的俱乐部用户的答案是我必须配置的其他东西
You need a table for an association between club and user to allow a user belong to multiple clubs.您需要一个表用于俱乐部和用户之间的关联,以允许用户属于多个俱乐部。 It is under that table you define the role of the user for that particular club.
在该表下,您可以定义该特定俱乐部的用户角色。
public class ClubMember
{
public string UserId {get;set;}
public int ClubId {get; set;}
public ApplicationUser User {get; set;}
public Club Club {get; set;}
public string RoleId {get;set;}
public IdentityRole Role {get; set;}
}
Remove List<Club> Clubs {get; set;}
删除
List<Club> Clubs {get; set;}
List<Club> Clubs {get; set;}
from ApplicationUser and also remove List<ApplicationUser> Members {get;set;}
from Club
List<Club> Clubs {get; set;}
从 ApplicationUser 中删除List<ApplicationUser> Members {get;set;}
从Club
Then you can add the following properties to ApplicationUser
and Club
然后您可以将以下属性添加到
ApplicationUser
和Club
public class ApplicationUser : IdentityUser
{
public virtual List<ClubMember> ClubMembers{get;set;}
}
public class Club
{
public virtual List<ClubMember> ClubMembers{get;set;}
}
Update更新
To assign a club to a user, it's important you do;要将俱乐部分配给用户,您必须这样做;
var clubmember = new ClubMember {UserId = userId, ClubId = clubId, RoleId = roleId};
_context.ClubMembers.Add(clubmember);
await _context.SaveChangesAsync();
To query all members in a club with Id = 7 for example, you could do this;例如,要查询 ID = 7 的俱乐部中的所有成员,您可以这样做;
var membersInClub = _context.ClubMembers.Include(c => c.User).Where(c => c.ClubId == 7).Select(c => c.User);
To query all clubs and their members查询所有俱乐部及其会员
var clubs = _conext.Clubs.Include(c.ClubMembers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.