WPF LINQ DataContext Refresh not reading new records in the sql database table

datacontextlinqrefreshwpf

I have a LINQ datacontext and a ObservableCollection holding records for a Companys table in SQL

Changes to the current records and new records made to the current datacontext (DataDC) are reflected when I do a

DataDC.SubmitChanges();

However if I have another Window with a separate DataContext to the same tables, if I modify fields for records that currently exist in both DataContexts (ie Company Name modified) these changes are seen when I do a

DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);

However if the OTHER window/datacontext created a new record or if I use SQL explorer to create a new record, these new records do not show in the DataContext.Company table even after a

DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);

I've tried different Modes.

So why doesn't a .Refresh(….) Load new records, but will reflect changes made to records that exist?

Am I missing something?

I can't see a way to just refresh the DC with completely all new data from the SQL tables?

Best Answer

The Refresh() method doesn't retrieve new records from the database.

I'm not entirely sure why that is - just one of those choices that the Linq to SQL product team made (presumably with good reasons). It could have something to do with the fact that the refresh method

is primarily designed for optimistic concurrency conflict resolution 1

To get the newly entered records you will need to either:

  • Reissue a new query on the same DataContext
  • Create a new instance of your DataContext

Which option you go with depends on context (no pun intended). Things to bear in mind are the intended Unit of Work nature of the Linq to SQL DataContext and the intended short life span of the Datacontext.


  1. Dinesh's Cyberstation - a Microsoft blog from a member of the Application Frameworks team.
Related Topic