实体框架 EF6 将数据添加到多对多关系表

本文关键字:关系 添加 框架 EF6 数据 实体 | 更新日期: 2023-09-27 18:34:48

我有一个多对多的关系,这将允许我在表格中添加多个投诉和员工。我对EF相当陌生。

这将比我已经拥有的更简单,但它将有助于回答我的问题:

员工:

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

抱怨:

   public class Complaint
    {
        public int Id { get; set; }
        public string Description { get; set; } 
    }

投诉人:

public class ComplaintXEmployee
{
    public ComplaintXEmployee()
    {
        Employees = new HashSet<Employee>();
        Complaints = new HashSet<Complaint>();
    }
    public int ComplaintXEmployeeId { get; set; }
    public int EmployeeId { get; set; }
    public int ComplaintId { get; set; }
    public virtual ICollection<Employee> Employees { get; set; }
    public virtual ICollection<Complaint> Complaints { get; set; } 
}

主要

    static void Main(string[] args)
    {
        var complaintList = new List<Complaint>()
        {
            new Complaint() {Description = "This is a Test"}
        };
        var employeeList = new List<Employee>()
        {
            new Employee() {FirstName = "John", Id = 1, LastName = "Doe"},
            new Employee() {FirstName = "Jane", Id = 2, LastName = "Doe"},
            new Employee() {FirstName = "Kid", Id = 3, LastName = "Smith"}
        };
        var c = new ComplaintXEmployee();
        //from here I dont know
        using (var context = new FakeEntities())
        {
            context.ComplaintXEmployees.Add(c);
            context.SaveChanges();
        }
    }

如何将这两个列表添加到投诉XEmployees?

实体框架 EF6 将数据添加到多对多关系表

您需要做的第一件事是将导航属性添加到实体。(我添加了一些额外的属性只是为了好玩(

[Table("Employee")] //can be Employees
public class Employee
{
    [Key]
    public int Id { get; set; }
    [StringLenth(64)]
    public string FirstName { get; set; }
    [StringLenth(64)]
    public string LastName { get; set; }
    public virtual ICollection<Complaint> Complaints { get; set; } 
}    
[Table("Complaint")] //can be Complaints
public class Complaint
{
    [Key]
    public int Id { get; set; }
    [StringLenth(128)]
    public string Description { get; set; } 
    public virtual ICollection<Employee> Employees { get; set; }
}

要手动定义中间表 - 在 DbContext 中,您可以像这样使用 FluentApi:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
            .Entity<Employee>()
            .HasMany(fa => fa.Complaints)
            .WithMany(u => u.Employees)
            .Map(m => m.MapLeftKey("EmployeeId")
                 .MapRightKey("ComplaintId")
                 .ToTable("ComplaintXEmployee"));
}

属性

[Key] 告诉实体框架它必须使用此"列"作为表的主键。

[StringLenth(128(] 告诉实体框架列长度为 128。最好指定长度,否则实体框架将假定它是NVARCHAR(MAX(。

[表("员工"(] 告诉实体框架要使用的表名。如果省略它,它将使用类名的复数形式。

无需

手动添加多对多表,因为 EF 将为你执行此操作:

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Complaint> Complaints { get; set; } 
}    
public class Complaint
{
    public int Id { get; set; }
    public string Description { get; set; } 
    public virtual ICollection<Employee> Employees { get; set; }
}

此类声明将自动生成表。在实体之间添加关系是微不足道的:

static void Main(string[] args)
{
    using (var context = new FakeEntities())
    {
        var complaintList = new List<Complaint>()
        {
            new Complaint() {Description = "This is a Test"}
        };
        Context.Complaint.AddRange(complaintList);
        var employeeList = new List<Employee>()
        {
            new Employee() {FirstName = "John", Id = 1, LastName = "Doe"},
            new Employee() {FirstName = "Jane", Id = 2, LastName = "Doe"},
            new Employee() {FirstName = "Kid", Id = 3, LastName = "Smith"}
        };
        Context.Employee.AddRange(employeeList);
        //Just add entities to corresponding collections.
        employeeList[0].Complaints.Add(complaintList[0]);
        context.SaveChanges();
    }
}
奇怪的是

,你在那里走了99%。

是因为您没有声明任何密钥和关系吗... 如果是这样,那么肖恩·索尔本的另一个答案更好!

static void Main(string[] args)
{
    var complaintList = new List<Complaint>()
    {
        new Complaint() {Description = "This is a Test"}
    };
    var employeeList = new List<Employee>()
    {
        new Employee() {FirstName = "John", Id = 1, LastName = "Doe"},
        new Employee() {FirstName = "Jane", Id = 2, LastName = "Doe"},
        new Employee() {FirstName = "Kid", Id = 3, LastName = "Smith"}
    };
    var c = new ComplaintXEmployee();
    c.Employees = employeeList;
    c.Complaints = complaintList;
    using (var context = new FakeEntities())
    {
        context.ComplaintXEmployees.Add(c);
        context.SaveChanges();
    }
}