views:

1083

answers:

3

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?

A: 

If you're having both windows in the same app, I don't see any reason not to use just one DataContext and pass a reference of your original datacontext to your second window.

Tony
Yes Ive been thinking that I need to do this, I see that having two datacontext to the same tables is not a good Idea and I will chnage this.HOWEVERE this does not solve the issue for when data is added or removed buy another user of the database.As the cached data in the SINGLE datacontext will still not show the externally added records. (and deleted, they thrown an "expeption Unable to refresh the specified object. The object no longer exists in the database")
PrimeTSS
+1  A: 

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.
David Hall
A: 

Appologies, Ive found out more.

The under lying Data Context DOES reflect the chnages live in the data base.

Its my fualt, I ASSUMED that

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

Refreashed the 'collection' (Companys) , I mean thats what intellisense actually says. But it doesnt. But if you clear the collection and then reload them as below, this works. The problem was not with the data collection, just that DataDC.Refresh doesnt do what I thought it did.

public void LoadCompanys(ProActiveDataClassesDataContext dataDC)
            // This loades the under lying datatables into the Observable collection
        {            
            this.Clear();
            foreach (Company thisCompany in dataDC.Companies)
            {
                this.Add(thisCompany);
            }
        }
PrimeTSS