简体   繁体   English

EF链接表有3个外键

[英]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? 如何先在EF代码中映射表格?

[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? 更新:我认为octavioccl是正确的,但我如何为用户分配Assoc? 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 . 使用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: 在这种情况下,您不需要在我上面显示的模型中使用任何属性(删除它们),只需覆盖上下文中的OnModelCreating方法并添加以下配置:

     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. EF将根据对象的关系生成FK。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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