Codehelp
Codehelp

Reputation: 4757

Updating data in two related tables using 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));
    ordersContext.SaveChanges();
}

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.

Upvotes: 3

Views: 1047

Answers (1)

Loic Lacomme
Loic Lacomme

Reputation: 315

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;

Upvotes: 0

Related Questions