Linq to Entities 使用 POCO 插入多对多关系的多个子关系记录
我有一个类似于下面的数据库:
Order
===============
OrderID
Description
EmployeeID
...other fields
Product
===============
ProductID
...other fields
OrderProducts
===============
OrderID
ProductID
Employee
===============
EmployeeID
...other fields
我使用的是 Linq to Entities,并且 edmx 文件是在没有 OrderProducts 表的情况下创建的,因为它只是一个连接表。产品表是静态产品列表 - 我目前不需要插入任何行。我可以使用以下代码成功插入订单表行:
[Serializable]
public class MyOrderObject
{
public int OrderID { get; set; }
public string OrderDescription { get; set; }
public int? EmployeeID { get; set; }
public IEnumerable<MyProductObject> ProductsList { get; set; }
...other fields
}
[Serializable]
public class MyProductObject
{
public int ProductID { get; set; }
...other fields
}
private static void AddNewOrder(MyOrderObject order)
{
using (var context = DatabaseHelper.CreateContext())
{
var dbOrder = new Order
{
OrderID = order.OrderID,
Description = order.OrderDescription,
Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
}
context.AddToOrders(dbOrder);
context.SaveChanges();
}
}
如何将我的子关系记录列表插入数据库? 我尝试过:
List<int> ProductIDs = order.ProductsList.Select(x => x.ProductID).ToList();
//dbOrder.Products.Attach(context.Products.Where(x => ProductIDs.Contains(x.ProductID)));
//or dbOrder.Products = context.Products.Where(x => ProductIDs.Contains(x.ProductID));
//or dbOrder.Products = context.Products.Contains(ProductIDs);
//or foreach(var p in order.ProductsList)
// {
// context.AttachTo("Products", new Product { ProductID = p.ProductID });
// }
I have a database similar to below:
Order
===============
OrderID
Description
EmployeeID
...other fields
Product
===============
ProductID
...other fields
OrderProducts
===============
OrderID
ProductID
Employee
===============
EmployeeID
...other fields
I'm using Linq to Entities and the edmx file has been created without the OrderProducts table, as it is just a joining table. The Products table is a list of static products - I dont need to insert any rows at the moment. The Order table I can insert rows successfully using the following code:
[Serializable]
public class MyOrderObject
{
public int OrderID { get; set; }
public string OrderDescription { get; set; }
public int? EmployeeID { get; set; }
public IEnumerable<MyProductObject> ProductsList { get; set; }
...other fields
}
[Serializable]
public class MyProductObject
{
public int ProductID { get; set; }
...other fields
}
private static void AddNewOrder(MyOrderObject order)
{
using (var context = DatabaseHelper.CreateContext())
{
var dbOrder = new Order
{
OrderID = order.OrderID,
Description = order.OrderDescription,
Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
}
context.AddToOrders(dbOrder);
context.SaveChanges();
}
}
How do I insert into the database my list of child relationship records??
I've tried:
List<int> ProductIDs = order.ProductsList.Select(x => x.ProductID).ToList();
//dbOrder.Products.Attach(context.Products.Where(x => ProductIDs.Contains(x.ProductID)));
//or dbOrder.Products = context.Products.Where(x => ProductIDs.Contains(x.ProductID));
//or dbOrder.Products = context.Products.Contains(ProductIDs);
//or foreach(var p in order.ProductsList)
// {
// context.AttachTo("Products", new Product { ProductID = p.ProductID });
// }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
ProductsList
类型更改为ICollection
。并确保它是 EDMX 映射模型的一部分。然后就可以添加产品了
You need to change the
ProductsList
type toICollection
. And make sure it is part of the EDMX mapped model.Then you can add products
无需更改对象类型:
No need to change the object type: