Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL does not update when data has changed in database

I have this problem where after a field (say Field3 in table MyTable) is updated on the database, MyTable.Field3 (in C#) is still returning the old value.

I suspect there is some caching...?

How do I force it to:
Read the value from the database?
OR
Update the value in the MyTable class?

Or is there anything I miss? I am new to LINQ

Thank you in advance.

like image 826
Aximili Avatar asked Apr 19 '10 02:04

Aximili


2 Answers

DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entity);

If you want to refresh your entire entity set, the easiest way is probably to just create a new DataContext and requery for everything.

like image 74
Jake Avatar answered Oct 02 '22 12:10

Jake


You're misunderstanding the model. Linq to SQL doesn't pass through to the database each time you ask for a member. When you fetch an object from the database, it gets stored in memory as a Linq to SQL object, and there it stands. You don't get an up-to-date version of the object until you query the database again.

like image 32
Dave Markle Avatar answered Oct 02 '22 13:10

Dave Markle