How to: Handle Relationships
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic will give you general guidelines about working with relationships in WCF Data applications.
Loading Related Data
By default, WCF Data Services limit the amount of data returned by a query. However, you can explicitly load related entities from the data service when it is needed. This section describes how to load such deferred content into your application.
When you execute a query, only entities in the addressed entity set are returned. For example, when a query against the Northwind data service returns Order entities, by default the related OrderDetails entities are not returned, even though there is a relationship between Order and OrderDetails. You can use the $expand query option to request that the query returns entities that are related by an association. Use the Expand method on the DataServiceQuery to add the $expand option to the query that is sent to the data service. You can request multiple related entity sets by separating them by a comma, as in the following example. The following example returns OrderDetails, Customers and Employees together with the Order entity set:
EntitiesModel dataManager = new EntitiesModel(new Uri("EntitiesModelService.svc", UriKind.Relative));
DataServiceQuery<Order> query = dataManager.Orders.Expand("Customer,Employee,OrderDetails");
query.BeginExecute(
s =>
{
DataServiceQuery<Order> state = s.AsyncState as DataServiceQuery<Order>;
ObservableCollection<Order> data = new ObservableCollection<Order>();
foreach (var entity in state.EndExecute(s))
data.Add(entity);
}, query);
Private Sub LayoutRoot_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
Dim dataManager As New EntitiesModel(New Uri("EntitiesModelService.svc", UriKind.Relative))
Dim query As DataServiceQuery(Of Order) = dataManager.Orders.Expand("Customer,Employee,OrderDetails")
query.BeginExecute(Sub(s)
Dim state As DataServiceQuery(Of Order) = TryCast(s.AsyncState, DataServiceQuery(Of Order))
Dim data As New ObservableCollection(Of Order)()
For Each entity In state.EndExecute(s)
data.Add(entity)
Next entity
End Sub, query)
End Sub
Creating Entities with Relationships
When you add a new entity in WCF Data Services, any relationships between the new entity and related entities are not automatically defined. This example demonstrates how to create and change relationships between entity instances.
EntitiesModel dataManager = new EntitiesModel(new Uri("EntitiesModelService.svc", UriKind.Relative));
private void Create()
{
// Load a specific category.
DataServiceQuery<Category> query = (from category in dataManager.Categories
where category.CategoryID == 1
select category) as DataServiceQuery<Category>;
query.BeginExecute(this.CategoryLoaded, query);
}
private void CategoryLoaded(IAsyncResult result)
{
DataServiceQuery<Category> state = result.AsyncState as DataServiceQuery<Category>;
Category relatedCategory = state.EndExecute(result).FirstOrDefault();
// Create and initialize a new product.
Product newProduct = new Product();
newProduct.Discontinued = false;
newProduct.ProductName = "New product";
// Add the new product to the service manager.
dataManager.AddToProducts(newProduct);
// Add links for the one-to-many relationship.
dataManager.AddLink(relatedCategory, "Products", newProduct);
// Add the new product to the collection and set the
// reference to the product.
relatedCategory.Products.Add(newProduct);
newProduct.Category = relatedCategory;
// Send the changes to the data service.
dataManager.BeginSaveChanges(SaveChangesOptions.ContinueOnError,
c =>
{
(c.AsyncState as EntitiesModel).EndSaveChanges(c);
}, dataManager);
}
Private dataManager As New EntitiesModel(New Uri("EntitiesModelService.svc", UriKind.Relative))
Private Sub Create()
' Load a specific category.
Dim query As DataServiceQuery(Of Category) = TryCast((
From category In dataManager.Categories
Where category.CategoryID = 1
Select category), DataServiceQuery(Of Category))
query.BeginExecute(AddressOf Me.CategoryLoaded, query)
End Sub
Private Sub CategoryLoaded(ByVal result As IAsyncResult)
Dim state As DataServiceQuery(Of Category) = TryCast(result.AsyncState, DataServiceQuery(Of Category))
Dim relatedCategory As Category = state.EndExecute(result).FirstOrDefault()
' Create and initialize a new product.
Dim newProduct As New Product()
newProduct.Discontinued = False
newProduct.ProductName = "New product"
' Add the new product to the service manager.
dataManager.AddToProducts(newProduct)
' Add links for the one-to-many relationship.
dataManager.AddLink(relatedCategory, "Products", newProduct)
' Add the new product to the collection and set the
' reference to the product.
relatedCategory.Products.Add(newProduct)
newProduct.Category = relatedCategory
' Send the changes to the data service.
dataManager.BeginSaveChanges(SaveChangesOptions.ContinueOnError, _
Sub(c) TryCast(c.AsyncState, EntitiesModel).EndSaveChanges(c), dataManager)
End Sub