tags:

views:

22

answers:

1

Suppose I have an Orders table in my database and a corresponding model class generated by the VS2008 "Linq to SQL Classes" designer. Suppose I also have a stored procedure (ProcessOrder) in my database that I use to do some processing on an order record.

If I do the following:

var order = dataContext.Orders.Where(o => o.id == orderId).First();

// More code here

dataContext.ProcessOrder(orderId);

order.Status = "PROCESSED";

dataContext.SubmitChanges();

...then I'll get a concurrency violation if the ProcessOrder stored proc has modified the order (which is of course very likely), because L2S will detect that the order record has changed, and will fail to submit the changes to that order.

That's all fairly logical, but what if I want to update the order record after calling the stored proc? How do I tell L2S to forget about its cached copy and refresh it from the DB?

+5  A: 

You can do it with the Refresh method on your data context, like so:

DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues,
                    DataContext.Orders);
klausbyskov
Perfect. I see that I can also specify a specific order record too, which is exactly what I need. Thanks!
Gary McGill