简体   繁体   中英

EF link table with 3 foreign keys

User can have access to many associations. For a given user, I need to assign a job role to each association that he has access to. How do I map the tables in EF code first?

[User] 
UserId

[Association]
AssocId

[Role]
RoleId

[LNK_User_Assoc_Role]
UserId   [PK]
AssocId  [PK]
RoleId   [FK]

Updated: I think octavioccl is correct, but how I assign Assoc to users? This is my code. It is not willing to work:

_user = new USER();
_user.Username = user.Username

DbContext.USERS.Add(_user);
DbContext.SaveChanges();

var newUser = DbContext.USERS.Find(_user.User_Id);                                               

foreach (int assocId in select2)
{
LNK_UserRoleAssoc u = new LNK_UserRoleAssoc();                           
u.User = newUser;
u.Association = DbContext.Associations.Find(assocId);
u.Role = DbContext.ROLES.Find(2);
newUser.UserRoleAssocs.Add(u);                          
}

DbContext.SaveChanges();

There are two ways to achieve what you need.

  1. Using Data Annotations :

     public class User{ [Key] public Guid UserId {get;set;} public virtual ICollection<UserRoleAssociation> UserRoleAssociations {get;set;} } public class Role{ [Key] public Guid RoleId {get;set;} public virtual ICollection<UserRoleAssociation> UserRoleAssociations {get;set;} } public class Association{ [Key] public Guid AssociationId {get;set;} public virtual ICollection<UserRoleAssociation> UserRoleAssociations {get;set;} } public class UserRoleAssociation{ [Key,ForeignKey("User"),Column(Order=1)] public Guid UserId {get;set;} [Key,ForeignKey("Role"),Column(Order=2)] public Guid RoleId {get;set;} [Key,ForeignKey("Associoation"),Column(Order=3)] public Guid AssociationId {get;set;} //Navigation Properties public virtual User User {get;set;} public virtual Role Role {get;set;} public virtual Association Association {get;set;} 

    }

  2. Using Fluent Api . In this case you don't need to use any attribute in the model I show above (delete them), just override the OnModelCreating method on your context and add these configurations:

     protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<UserRoleAssociation>() .HasKey(c => new {c.UserId, c.RoleId, c.AssociationId}); modelBuilder.Entity<UserRoleAssociation>() .HasRequired(p => p.User) .WithMany(u => u.UserRoleAssociations) .HasForeignKey(p => p.UserId); modelBuilder.Entity<UserRoleAssociation>() .HasRequired(p => p.Role) .WithMany(r => r.UserRoleAssociations) .HasForeignKey(p => p.RoleId); modelBuilder.Entity<UserRoleAssociation>() .HasRequired(p => p.Association) .WithMany(a => a.UserRoleAssociations) .HasForeignKey(p => p.AssociationId); } 

EF will generate the FK's based on the relationship of the objects.

public class User{
    public Guid UserId {get;set;}
    public Role Role {get;set;}
    public Association Association {get;set;}
}

public class Role{
    public Guid RoleId {get;set;}
}

public class Association{
    public Guid AssociationId {get;set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM