[英]Entity Framework (EF6) + MySql Database First Model Many to Many Relationship Wrong Query Generation
[英]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.