【问题标题】:Linq to Entities insert multiple child relationship records for many-to-many relationship using POCOLinq to Entities 使用 POCO 为多对多关系插入多个子关系记录
【发布时间】:2012-01-17 09:00:36
【问题描述】:

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

Order
===============
OrderID
Description
EmployeeID
...other fields


Product
===============
ProductID
...other fields


OrderProducts
===============
OrderID
ProductID


Employee
===============
EmployeeID
...other fields



我正在使用 Linq to Entities,并且创建的 edmx 文件没有 OrderProducts 表,因为它只是一个连接表。 Products 表是静态产品列表 - 我现在不需要插入任何行。 Order 表我可以使用以下代码成功插入行:

[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 });
//   }

【问题讨论】:

    标签: c# asp.net entity-framework linq-to-entities many-to-many


    【解决方案1】:

    您需要将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);
    

    【讨论】:

    • 我没有长时间使用 EF4,所以我不明白 - 为什么要将数据库对象添加到我的 POCO 对象?那如何将子关系插入数据库?
    • 如果我将其保留为 IEnumerable 并将最后一行更改为 dbOrder.Products.Add(p);有用! :)
    • @JumpingJezza 什么会添加一个新的Product。你不想添加OrderProduct吗?
    • dbOrder.Products.Add(p) 添加了 OrderProduct,而不是 Product - 我已经测试过了。虽然它看起来并不直观。
    【解决方案2】:

    无需更改对象类型:

    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();
    

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多