c# - 带有 3 个外键的 EF 链接表

标签 c# entity-framework

用户可以访问许多协会。对于给定的用户,我需要为他有权访问的每个关联分配一个工作角色。我如何首先映射 EF 代码中的表?

[User] 
UserId

[Association]
AssocId

[Role]
RoleId

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

更新: 我认为 octavioccl 是正确的,但我如何将 Assoc 分配给用户?这是我的代码。它不愿意工作:

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

最佳答案

有两种方法可以实现您的需求。

  1. 使用 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. 使用 Fluent Api .在这种情况下,您不需要在我上面显示的模型中使用任何属性(删除它们),只需在您的上下文中覆盖 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);
    }
    

关于c# - 带有 3 个外键的 EF 链接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482452/

相关文章:

c# - 序列化为 JSON 时排除集合中的特定项目

c# - Breeze.js 中的计数

c# - 如何在 Entity Framework 中使用流利的 api 为一对零指定外键属性..一个关系

c# - 检测输入是否为格式化数字的正则表达式

c# - 将秒数转换为天数,hh :mm:ss C#

c# - 将 Autofac 与使用 WAS 的 WCF 服务结合使用

c# - 尝试从列序号 '0' 读取无效

c# - Azure 容器应用服务。 Docker 运行参数?

c# - 使用 Entity Framework Core 调用标量函数的最佳实践 (2.1)

entity-framework - EF6 中是否修复了 TPT 继承的性能问题?