How do I get Linq-to-SQL to refresh its local copy of a database record?

Posted by Gary McGill on Stack Overflow See other posts from Stack Overflow or by Gary McGill
Published on 2010-04-07T13:37:05Z Indexed on 2010/04/07 13:43 UTC
Read the original article Hit count: 377

Filed under:

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?

© Stack Overflow or respective owner

Related posts about linq-to-sql