使用GraphDiff更新两个相关表中的数据

时间:2014-11-05 06:34:24

标签: c# entity-framework graphdiff

我有两个表,Order和OrderItems

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

对于给定的订单,如果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

任何线索?

提前致谢。

1 个答案:

答案 0 :(得分:0)

异常意味着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

然后在那种情况下,我认为该对象仍然附有异常;

相关问题