使用Code First为3个表之间的多对多关系建模

SO *_*ood 5 c# entity-framework

我有以下3个实体:

  • 用户
  • 帐户
  • 角色

关系就像

  • 一个用户可以拥有多个帐户
  • 一个帐户可以属于多个用户
  • 每个用户在一个帐户中都有一个角色
  • 有一些预定义的角色(在“枚举角色”中定义)

我已经走了这么远:

public class User
{
    public int Id { get; set; }
    public ICollection<Account> Accounts { get; set; }
}

public class Account
{
    public int Id { get; set; }
    public ICollection<User> Users { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Account> Accounts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

每次用户注册时,他们也会创建一个帐户,因此我想到了这一点:

public async Task AddAccountAsync(User user, string accountName)
{
    Account account = new Account(user, accountName);
    Role role = new Role(Roles.Owner);
    Accounts.Add(account);
    user.Accounts.Add(account);
    Entry(user).State = EntityState.Modified;
    await SaveChangesAsync();
 }
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何将角色与用户绑定到帐户,因此我可以获取用户在帐户中拥有的角色。

例如:

考虑3个用户和3个帐户:

  • 帐户1 =(用户1,所有者),(用户2,TeamMember)
  • 帐户2 =(用户2,所有者)
  • 帐户3 =(用户3,所有者),(用户1,ReadOnlyMember)

遵循@IvanStoev的回答,我得到了:

public async Task AddAccountAsync(User user, string accountName)
{
   Role role = new Role(Roles.Owner);
   Account account = new Account(model.AccountCurrency, model.AccountName, model.Description);
   UserAccount uc = new UserAccount
   {
       User = user,
       Account = account,
       Role = role
   };
   account.UserAccounts.Add(uc);
   Accounts.Add(account);

   user.UserAccounts.Add(uc);
   Entry(user).State = EntityState.Modified;
   await SaveChangesAsync();
}
Run Code Online (Sandbox Code Playgroud)

是否应该保存UserAccount对象(作为DbContext<UserAccount>)?

Iva*_*oev 7

  • 一个用户可以拥有多个帐户
  • 一个帐户可以属于多个用户
  • 每个用户在一个帐户中都有一个角色

当您需要将其他信息(Role在这种情况下)与User - Account链接关联时,自动链接表不适用于这种情况。

因此,除了自动many-to-many关联之外,您需要使用两个one-to-many具有显式链接实体的关联,如下所示:

模型:

public class User
{
    public int Id { get; set; }
    public ICollection<UserAccount> UserAccounts { get; set; }
}

public class Account
{
    public int Id { get; set; }
    public ICollection<UserAccount> UserAccounts { get; set; }
}

public class UserAccount
{
    public int UserId { get; set; }
    public int AccountId { get; set; }
    public int RoleId { get; set; }
    public User User { get; set; }
    public Account Account { get; set; }
    public Role Role { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    public ICollection<UserAccount> UserAccounts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

组态:

modelBuilder.Entity<UserAccount>()
    .HasKey(e => new { e.UserId, e.AccountId });

modelBuilder.Entity<UserAccount>()
    .HasRequired(e => e.User)
    .WithMany(e => e.UserAccounts)
    .HasForeignKey(e => e.UserId);

modelBuilder.Entity<UserAccount>()
    .HasRequired(e => e.Account)
    .WithMany(e => e.UserAccounts)
    .HasForeignKey(e => e.AccountId);

modelBuilder.Entity<UserAccount>()
    .HasRequired(e => e.Role)
    .WithMany(e => e.UserAccounts)
    .HasForeignKey(e => e.RoleId);
Run Code Online (Sandbox Code Playgroud)

您可以UserAccount通过多种方式创建新商品。

一种方法是将其添加到子集合之一UserAccounts。在你的榜样,account.UserAccounts.Add(uc);随后context.Accounts.Add(account)会自动将其添加到context.UserAccountsuser.UserAccountsrole.UserAccounts

另一种方法是使用context.UserAccounts.Add(uc);在这种情况下,它会自动添加到useraccountrole孩子UserAccounts集合。