Updating data in two related tables using GraphDiff
I have two tables: Order and OrderItems
The Order table has an OrderId column in which the primary key OrderItems also has this column as the foriegn key.
For a given order, if the OrderId is 1 and has two items, the OrderItems table will have two rows, each with an OrderID of 1.
Using EF, I created a context with two tables.
The Order table and OrderItems table now have a Status column.
With GraphDiff, I would like to update this value like 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 hints?
Thanks in advance.
source to share
An exception means that it is likely that orderToUpdate or its associated properties are already bound to a context or another context instance. You need to look at how you retrieve or create the 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 this case I believe the object is still bound due to the exception;
source to share