[英]Code first add entity with many-to-many relationship
I have an object Customer that can have multiple customer types and each customer type can have multiple customers. 我有一个对象Customer,可以有多个客户类型,每个客户类型可以有多个客户。 I'm new to EF but have managed to add a customer, but can't seem to get the syntax right for adding the customer's customer types as well.
我是EF的新手,但是设法添加了一个客户,但是似乎也没有正确地添加客户的客户类型的语法。
My customer class (simplified): 我的客户类别(简体):
public partial class Customer
{
public virtual int Id { get; set;}
public virtual string Name { get; set;}
#region Navigation Properties
public virtual ICollection<CustomerType> CustomerTypes
{ get; set; }
#endregion
}
Customer type: 客户类型:
public partial class CustomerType
{
public virtual int Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
#region Navigation Properties
public virtual ICollection<Customer> Customers
{ get; set; }
#endregion
}
When I run this project a CustomerTypeCustomer table is created with columns Customer_Id and CustomerType_Id so this is fine. 当我运行此项目时,将使用Customer_Id和CustomerType_Id列创建一个CustomerTypeCustomer表,因此很好。
I then create the customer like so: 然后,我像这样创建客户:
// code behind
var customer = new Customer();
customer.name = txtCustomerName.Text;
using (var context = new MyEntities())
{
context.Customers.Add(customer);
context.SaveChanges();
}
I had a look here Insert/Update Many to Many Entity Framework . 我在这里看到了“ 插入/更新多对多实体框架”。 How do I do it?
我该怎么做? and tried to do something similar with customer types:
并尝试对客户类型执行类似的操作:
var customer = new Customer();
customer.name = txtCustomerName.Text;
// only starting with one customer type selected in a check box list
CustomerType customerType = context.CustomerTypes.FirstOrDefault(i => i.Id == 1);
using (var context = new MyEntities())
{
// IncidentTypes throws Object reference not set to an instance of an object
customer.CustomerTypes.add(customerType);
context.Customers.Add(customer);
context.SaveChanges();
}
Am I missing something obvious here? 我在这里错过明显的东西吗?
Thanks in advance. 提前致谢。
Edit: for some reason I only have .Add( no AddToObject, AttachTo etc. 编辑:由于某种原因,我只有.Add(没有AddToObject,AttachTo等。
You must initialize CustomersTypes
collection first. 您必须首先初始化
CustomersTypes
集合。
customer.CustomerTypes = new List<CustomerType>();
You can also add this initialization to your Customer
's constructor. 您还可以将此初始化添加到
Customer
的构造函数中。
This is one way to do it: 这是一种方法:
var customer = new Customer();
customer.name = txtCustomerName.Text;
// only starting with one customer type selected in a check box list
//add Include there
CustomerType customerType = context.CustomerTypes.Include("Customers").FirstOrDefault(i => i.Id == 1);
using (var context = new MyEntities())
{
// IncidentTypes throws Object reference not set to an instance of an object
//customer.CustomerTypes.add(customerType);
//context.Customers.Add(customer);
customerType.Customers.Add(customer);
context.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.