繁体   English   中英

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

[英]Entity Framework EF6 Add data to Many to Many relationship Table

我有很多对很多的关系,这使我可以向表中添加多个投诉和员工。 我对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?

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

[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)。

[Table(“ Employee”)]告诉实体框架您要使用的表名。 如果不考虑这一点,它将使用类名的复数形式。

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

暂无
暂无

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

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