Reputation: 946
I have a silverlight 4 application which uses RIA with EF to query multiple tables in a single DomainContext. BUGroup, BUGroupBuilding and vwBusinessUnit.
The UI basically loads the BUGroup entity set and I can select different BUGroups and it would load child tables like this:
I have a DomainContext that I'm passing to a childwindow in the Manage Buildings button like this:
ManageBuildingsChildWindow ManageBuildingscw = new ManageBuildingsChildWindow();
ManageBuildingscw.Closed += new EventHandler(ManageBuildingscw_Closed);
ManageBuildingscw.DataContext = null;
ManageBuildingsViewModel ManageBuildingsViewModel = new ManageBuildingsViewModel();
ManageBuildingscw.DataContext = ManageBuildingsViewModel;
and then I'm loading the childwindow context in the childwindow view model like this:
GetBUGroupResult = SecurityDomainContext.Current.Load(SecurityDomainContext.Current.GetBUGroupsCustomQuery(), LoadBehavior.RefreshCurrent, false);
GetBUGroupResult.Completed += new EventHandler(GetBUGroupResult_Completed);
here's the event handler for GetBUGroupResult
void GetBUGroupResult_Completed(object sender, EventArgs e)
{
GetBUGroupBuildings = SecurityDomainContext.Current.BUGroupBuildings.Where(w => w.BUGroupID == BUGroupID).ToList();
GetBUGroupResult.Completed -= new EventHandler(GetBUGroupResult_Completed);
}
I bind each BUGroupBuilding to a delete link in a datagrid and it deletes from database fine. when I click on the manage building button to invoke the child window and it loads fine for the first time. if i have 5 buildings, it loads 5 buildings. the problem is when i loads it 2nd or other times after deleting a few buildings. it retains the old DomainContext even after the load. I even try setting the LoadBehavior to RefreshCurrent on the Load for the GetBUGroupsCustomQuery()
say I have 5 buildings in a group and i deleted 2 on parent window using the delete link so now i have 3. invokes the childwindow. it still shows 5.
Now I put a break on the DomainServices for GetBUGroupsCustomQuery() and i get the correct 3 value coming back
But during the GetBUGroupResult_Completed event handler, I'm seeing 5 buildings still. It looks like my DomainContext is not refreshing even when I specified the loadbehavior to refresh current. any input?
Upvotes: 2
Views: 1401
Reputation: 109185
You can also do:
var c = SecurityDomainContext.Current;
var group = c.BUGroups.Single(w => w.BUGroupID == BUGroupID);
c.Refresh(RefreshMode.StoreWins, group.BUGroupBuildings);
GetBUGroupBuildings = group.BUGroupBuildings.ToList();
By RefreshMode.StoreWins
it is guaranteed that the current state of the database is retrieved.
Upvotes: 1
Reputation: 370
I had a similar problem to this and it was solved with a workaround that loads data into the context and then detaches any objects in the entity collection that is not in the collection of the newly returned objects. Try something like this with your load operation:
SecurityDomainContext.Current.Load<YourObjectType>(
SecurityDomainContext.Current.GetBUGroupsCustomQuery(),
LoadBehavior.MergeIntoCurrent,
loadOperation =>
{
var results = context.Comments.Where(
entity => !loadOperation.Entities.Contains(entity)).ToList();
results.ForEach(entity => context.Comments.Detach(entity));
}, null);
I'm not sure if you'll need to replace <YourObjectType>
with the entity type that is returned, or if you can just remove that part, but this should at least get you close.
Upvotes: 1