如何将桥表拆分为EF核心中的两个表

时间:2018-01-23 00:09:38

标签: c# entity-framework asp.net-core

我有一个要求,我将桥接表滑入表格,以存储有关多对多关系的更多信息。所以“想法”是计算机用户表变成两个表并以这种方式引用,这样除了多对多关系引用之外,可以在这两个桥表中存储更多信息。感谢您的见解以下是我所拥有的:

public  class Computer
{

[Key]
    public Guid ComputerId { get; set; }
    public string MachineName { get; set; }
    public DateTime Updated { get; set; }
    public string ClientVersion { get; set; }
    public DateTime LastActivity { get; set; }
    public string Secret { get; set; }
    public Setting Settings { get; set; }
    public bool IsActive { get; set; } 
    public bool? ResetRequested { get; set; } 
    public ICollection<ScanEvent> Scans { get; set; }
    public ICollection<EventLog> Logs { get; set; }
    public ICollection<ComputerUser> UserLinks { get; set; }
    }
public class ComputerUser
{
    [Key,Column(Order = 1)]
    public Guid ComputerId { get; set; }
    public  Computer Computer { get; set; }
    [Key, Column(Order = 2)]
    public Guid  UserId { get; set; }
    public  User User { get; set; }

}

    public class User
 {
    [Key]

   public Guid   UserId { get; set; } 
   public  string Name { get; set; }
   public bool IsActive { get; set; } = true;
   public DateTime Updated { get; set; } 
   public Setting Setting { get; set; } 
   public long SerialNumber { get; set; } 
   public virtual ICollection<Device> Devices { get; set; }
   public virtual ICollection<Credential> Credentials { get; set; }
   public virtual ICollection<ComputerUser> ComputerLinks { get; set; }
   }

1 个答案:

答案 0 :(得分:0)

您所谈论的只是具有有效载荷的M2M(即除了关系本身之外的其他信息)。唯一的要求是为您的关系使用实际的实体类,您已经已经使用ComputerUser。只需根据需要向该实体添加其他属性即可。