. NET用户的核心身份分组



我目前有一个使用身份的项目,单个用户可以访问自己的资源(由应用程序用户id链接的子数据库表)。我希望每个注册的用户(管理员)都能邀请新用户加入他们的"组",并共享相同的资源。

我正在考虑实现一个"组"one_answers"UserGroups"表类似于当前的"角色"one_answers"UserRoles"表。下面的代码看起来可以实现这一点吗?

public class ApplicationUser : IdentityUser
{
[MaxLength(30)]
public string FirstName { get; set; }
[MaxLength(30)]
public string LastName { get; set; }
[MaxLength(100)]
public string Company { get; set; }
[MaxLength(30)]
public string Telephone { get; set; }
[MaxLength(15)]
public string Postcode { get; set; }
public DateTime Created { get; set; } = DateTime.UtcNow;
public DateTime PasswordResetTime { get; set; } = DateTime.UtcNow;
public bool PasswordReset { get; set; } = false;
public virtual ICollection<GSMSite> GSMSites { get; set; }
}
public class UserGroups
{
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual Groups Groups { get; set; }
}
public class Groups
{
public Guid Id { get; set; }
[MaxLength(30)]
public string Name { get; set; }
}

您正在尝试实现多对多关系。首先,按照惯例,你应该用单数形式命名类:

  • 用户组UserGroup
  • 组组

您还应该将ICollection放在applicationuser和Group:

public virtual ICollection<UserGroup> UserGroups

并将UserGroup更改为:

public class UserGroup
{
public Guid ApplicationUserId { get; set;}
public virtual ApplicationUser ApplicationUser { get; set; }
public Guid GroupId { get; set; }
public virtual Groups Groups { get; set; }
}

然后你正在谈论的资源应该链接到Group而不是ApplicationUser

最新更新