2016-07-24 62 views
3

我有以下3個實體:建模與3個表之間的許多一對多的關係的Code First

  • 用戶
  • 帳戶
  • 角色

而且關係是這樣

  • 一個用戶可以有多個帳戶
  • 一個帳戶可以屬於多個用戶
  • 每個用戶都擁有一個帳戶的角色

我走到這一步了幾下,預定義角色(在枚舉角色定義):

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; } 
} 

每次用戶註冊,他們也創造了一個帳戶,所以我想到了這一點:

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(); 
} 

但我不確定如何將角色與用戶綁定到該帳戶,以便我可以獲取用戶在帳戶中的角色。

例如:

考慮3個用戶和3個帳戶:

  • 帳戶1 =(用戶1,用戶),(用戶2,TeamMember)
  • 帳戶2 =(用戶2,用戶)
  • Account3 =(User3,Owner),(User1,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(); 
} 

我應該保存UserAccount對象(作爲DbContext<UserAccount>)嗎?

+0

用戶可以擁有沒有角色的帳戶嗎?你應該提供更多的細節,如果你提供了你想要的東西的例子,那會更好。 –

+0

@TaherRahgooy不,用戶需要在帳戶中扮演角色。我會添加一個例子 –

回答

4
  • 一個用戶可以擁有多個賬戶
  • 一個帳戶可以屬於多個用戶
  • 每個用戶都擁有一個帳戶的角色

自動鏈接表不起作用對於需要將其他信息(在這種情況下爲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; } 
} 

配置:

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); 

你可以創建一個新的UserAccount在幾個方面。

一種方法是將其添加到其中一個子集合UserAccounts。在你的例子中,account.UserAccounts.Add(uc);後跟context.Accounts.Add(account)會自動將它加到context.UserAccounts,user.UserAccountsrole.UserAccounts

另一種方法是使用context.UserAccounts.Add(uc);在這種情況下,它會自動添加到useraccountrole孩子UserAccounts集合。

+0

這看起來不錯。您能否添加一個我如何創建新用戶/賬戶的例子?我有一個猜測,但我想確保 –

+0

這足以將'UserAccount'添加到其中一個子集合(就像通常的'一對多'情況一樣)。在你的示例中,'account.UserAccounts.Add(uc);'應該足夠了。 –

+0

謝謝你的回答,這就是我一直在尋找的 –

相關問題