【问题标题】:Updating data in two related tables using GraphDiff使用 GraphDiff 更新两个相关表中的数据
【发布时间】:2014-11-05 06:34:23
【问题描述】:

我有两张表,Order 和 OrderItems

Order 表有一个 OrderId 列,它是主键 OrderItems 也有此列作为外键。

对于给定的订单,如果 OrderId 为 1 并且它有两个项目,则 OrderItems 表 将有两行,每行的 OrderID 为 1。

我使用 EF 为这两个表创建了一个上下文。

现在 Order 表和 OrderItems 表都有一个 Status 列。

使用 GraphDiff 我想像这样更新这个值:

using (var ordersContext = new OrdersContext())
{
    ordersContext.UpdateGraph(orderToUpdate, map => map.OwnedCollection(p => p.OrderItems));
    ordersContext.SaveChanges();
}

这给出了以下异常:

GraphDiff supports detached entities only at this time. Please try AsNoTracking() or detach your entites before calling the UpdateGraph method

有什么线索吗?

提前致谢。

【问题讨论】:

    标签: c# entity-framework graphdiff


    【解决方案1】:

    异常意味着 orderToUpdate 或相关属性很可能已附加到上下文或上下文的另一个实例。在调用此代码之前,您需要查看如何检索或生成 orderToUpdate。

    例如,如果你这样做

    var ordersContext = new OrdersContext();
    var orderToUpdate = ordersContext.Find(orderToUpdateId); // id of what is looked for
    orderToUpdate.DateCreated = DateTime.Now; // any sort of update
    

    那么在这种情况下,我相信该对象仍因异常而被附加;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多