How to refresh a LINQ to Entities Query
I have the following LINQ to Entities code:
var vans = from v in objDB.vans.Include("van_assignments.pickup_requests.to_location")
.Include("van_assignments.pickup_requests.from_location")
.Include("van_assignments.pickup_requests.person")
.Include("school")
select v;
gcVans.DataSource = vans;
Now, in another method (ie a refresh button), I am needing to refresh that query 开发者_运维百科as changes were made to the remote DB.
I tried calling the above code again, but it doesn't refresh the data. Does anyone know of a best practice for forcing LINQ to Entities to refresh from the remote database?
Edit: After a bit of dabbling, this causes the van information to update. However, all the foreign key data that was loaded with the .include() function does not update. Does anyone have any idea why?
var vans = from v in objDB.vans.Include("van_assignments.pickup_requests.to_location")
.Include("van_assignments.pickup_requests.from_location")
.Include("van_assignments.pickup_requests.person")
.Include("school")
select v;
objDB.Refresh(RefreshMode.StoreWins, vans);
gcVans.RefreshDataSource();
It sounds like you're using the wrong MergeOption.
The default value, AppendOnly
, tries to preserve client-side changes and doesn't overwrite on a refresh. Perhaps you want OverwriteChanges
? Then you would not need to call Refresh
.
To set this, you'd do something like:
(vans as ObjectQuery).MergeOption = MergeOption.OverwriteChanges;
精彩评论