I am developing an application using WPF/MVVM. I have a WCF data service project which provides operations for retrieving data from an ADO.NET data model using entity framework. I then have a wpf client which binds to viewmodel properties that fetch from the WCF service. The scenario involves clients/offices. The client entity has a navigation property of type office, as is implemented as a foreign key in the database. The problem is that when my viewmodel gets the list of clients from the data service, the navigation property is null. The service operation however does retrieve this information.
WCF Service Operation
[OperationContract]
public IEnumerable<Client> GetClientsByOffice(int officeID)
{
using (var context = new LDC_Entities())
{
var result = context.Clients.Include("Registered_Office")
.Where(c => c.Registered_Office_ID == officeID).ToList();
result.ForEach(c => context.Detach(c));
return result;
}
}
As you can see the office property is loaded within the context query. If I put a breakpoint in at this point, the result variable holds the clients information, and the navigation property is also filled as expected.
WPF ViewModel
private void RefreshClients()
{
serviceClient.GetClientsByOfficeCompleted += (s, e) =>
{
Clients = e.Result;
foreach (Client c in Clients)
MessageBox.Show(c.Office.City);
};
this.serviceClient.GetClientsByOfficeAsync(CurrentOffice.Office_ID);
}
If I inspect the Clients property after this method is called, the navigation property is now empty, and as such the message box call I put in throws a null pointer exception. It appears that as it comes through the WCF service, it drops the navigation properties of the client objects.
Please could anyone explain how this information can be retained when making this call?
Many thanks in advance, Mike