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

时间:2016-02-15 12:09:01

标签: c# entity-framework

我有很多关系可以让我在桌子上添加多个投诉和员工。我是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; } 
    }

ComplaintXEmployee:

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

如何将这两个列表添加到ComplaintXEmployees?

3 个答案:

答案 0 :(得分:1)

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

[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]告诉实体框架它必须使用这个&#34;列&#34;作为表的主键。

[StringLenth(128)]告诉实体框架列长度为128.指定长度总是更好,否则实体框架将假设其为NVARCHAR(MAX)。

[表(&#34;员工&#34;)]告诉实体框架您要使用的表名。如果你把它留下来,它将使用复数形式的类名。

答案 1 :(得分:0)

您不需要手动添加多对多表,因为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();
    }
}

答案 2 :(得分:0)

很奇怪你就像99%那样。

是因为你没有宣布任何关键和关系......  如果是这样,那么Sean Thorburn的另一个答案就更好了!

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