[英]CRUD operation while working with many to many relationships in EF-Core 2
[英]Many to many relationships EF Core 5.0.3
我需要为我自己的站点复制不和谐用户 model 并且规则是。
一个用户可以是多个俱乐部的一部分这些俱乐部中的每个用户都可以具有管理员或管理员的角色。
到目前为止,我有网站范围的角色在工作,但不在俱乐部实体内,我无法将我的俱乐部与用户联系起来
这就是我目前使用的 ef core 5.0.3,如果这有所作为的话。
应用用户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; }
}
然后在我的俱乐部里,我有
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; }
}
到目前为止我试过这个
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);
}
但它只给了我一个管理员用户,而不是俱乐部中的角色。 俱乐部的映射只允许每个成员一个俱乐部,当他们可能是多个俱乐部的一部分时,它与 discord 的原理完全相同,其中频道是俱乐部,用户可以是模组或普通用户。
然后在我看来我要回来了。
var club = await _context.Clubs.Include(c =>c.Members).ToListAsync();
return View(club);
我希望看到每个俱乐部有很多成员,但每个成员在任期内可能属于不同的俱乐部多次
编辑 2 仍然存在问题
我仍然从下面的人那里得到零计数我的俱乐部用户的答案是我必须配置的其他东西
您需要一个表用于俱乐部和用户之间的关联,以允许用户属于多个俱乐部。 在该表下,您可以定义该特定俱乐部的用户角色。
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;}
}
删除List<Club> Clubs {get; set;}
List<Club> Clubs {get; set;}
从 ApplicationUser 中删除List<ApplicationUser> Members {get;set;}
从Club
然后您可以将以下属性添加到ApplicationUser
和Club
public class ApplicationUser : IdentityUser
{
public virtual List<ClubMember> ClubMembers{get;set;}
}
public class Club
{
public virtual List<ClubMember> ClubMembers{get;set;}
}
更新
要将俱乐部分配给用户,您必须这样做;
var clubmember = new ClubMember {UserId = userId, ClubId = clubId, RoleId = roleId};
_context.ClubMembers.Add(clubmember);
await _context.SaveChangesAsync();
例如,要查询 ID = 7 的俱乐部中的所有成员,您可以这样做;
var membersInClub = _context.ClubMembers.Include(c => c.User).Where(c => c.ClubId == 7).Select(c => c.User);
查询所有俱乐部及其会员
var clubs = _conext.Clubs.Include(c.ClubMembers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.