Linq to Entities 使用 POCO 插入多对多关系的多个子关系记录

发布于 2024-12-27 18:27:52 字数 1774 浏览 1 评论 0原文

我有一个类似于下面的数据库:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2025-01-03 18:27:52

您需要将 ProductsList 类型更改为 ICollection。并确保它是 EDMX 映射模型的一部分。

public class MyOrderObject
{
   public int OrderID { get; set; }
   public string OrderDescription { get; set; }
   public int? EmployeeID { get; set; }
   public ICollection<MyProductObject> ProductsList { get; set; }
   ...other fields
}

然后就可以添加产品了

var products = context.Products.Where(/**/);
foreach(var p in products)
   order.ProductsList.Add(p);

You need to change the ProductsList type to ICollection. And make sure it is part of the EDMX mapped model.

public class MyOrderObject
{
   public int OrderID { get; set; }
   public string OrderDescription { get; set; }
   public int? EmployeeID { get; set; }
   public ICollection<MyProductObject> ProductsList { get; set; }
   ...other fields
}

Then you can add products

var products = context.Products.Where(/**/);
foreach(var p in products)
   order.ProductsList.Add(p);
薯片软お妹 2025-01-03 18:27:52

无需更改对象类型:

var dbOrder = new Order
{
   OrderID = order.OrderID,
   Description = order.OrderDescription,
   Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
}

//this adds the relationship to the child without adding a new child record - perfect!
foreach(var p in order.ProductsList)
{
   dbOrder.Products.Add(p);
}

context.AddToOrders(dbOrder);
context.SaveChanges();

No need to change the object type:

var dbOrder = new Order
{
   OrderID = order.OrderID,
   Description = order.OrderDescription,
   Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
}

//this adds the relationship to the child without adding a new child record - perfect!
foreach(var p in order.ProductsList)
{
   dbOrder.Products.Add(p);
}

context.AddToOrders(dbOrder);
context.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文