Updating data in two related tables using GraphDiff

c# entity-framework graphdiff


I have two tables, Order and OrderItems

The Order table has a OrderId column that's the primary key The OrderItems also has this column as foriegn key.

For a given Order, if the OrderId is 1 and it has two items the OrderItems table will have two rows each having OrderID as 1.

Using EF I created a context with the two tables.

Now the Order table and OrderItems table both have a Status column.

Using GraphDiff I wanted to update this value likes so:

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

This gives the following exception:

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

Any clues?

Thanks in advance.

11/5/2014 6:34:23 AM

Popular Answer

The exception means most likely that orderToUpdate or related properties are attached to the context or another instance of the context already. You need to look at how you retrieve or produce orderToUpdate before calling this code.

for example if you do

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

then in that case I believe the object is still attached due the exception;

3/4/2015 3:37:40 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow