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

后端 未结 2 1617
野趣味
野趣味 2020-12-11 15:27

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.

相关标签:
2条回答
  • 2020-12-11 15:35
    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.

    0 讨论(0)
  • 2020-12-11 15:45

    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.

    0 讨论(0)
提交回复
热议问题