我有3个表,实体框架添加记录到多对多映射表
1)客户(ID,姓名,血乳酸血乳酸)
2)CustomerGroups(的GroupId,组名)
3 )CustomerInGroups(CustomerId,GroupId)
using (var context = DataObjectFactory.CreateContext())
{
context.Customers.Add(entity);
context.SaveChanges();
return entity.Id;
}
如何将记录添加到CustomerInGroups中?的EntityFramework不会产生这样的许多一对多的映射表
实体编辑:
无论是在客户ID列和CustomerGroups被设置为自动增量。
所以在我CustomersGroup表,我有
Id Name
----------------------------
1 Normal
2 VIP
我试着这样做的海报的一个建议:
entity.CustomerGroups = new List<CustomerGroup>
{
new CustomerGroup {Id = 2 }
};
context.Customers.Add(entity);
context.SaveChanges();
return entity.Id;
但是,我这样做的时候,而不是建立在一个记录映射表如下:
CustomerId GroupId
----------------------------
1 2
我得到的是
CustomerInGroups
CustomerId GroupId
----------------------------
1 3
CustomerGroups
Id Name
----------------------------
1 Normal
2 VIP
3 NULL
它实际上创造了我的CustomerGroups表,这是不是我想要
实体框架在您将客户分配给组时是否不会自动更新连接表?如果连接没有有效负载(即,仅连接外键) – Daniel 2013-03-07 04:21:15