Share via


How to: Define Entity Relationships (ADO.NET Data Services)

When you add a new entity, any relationships between the new entity and related entities are not automatically defined. You can create and change relationships between entity instances and have the client library reflect those changes in the data service. For more information, see Updating the Data Service (ADO.NET Data Services).

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the ADO.NET Data Services quickstart.

Example

The following example creates a new object instance and then calls the AddRelatedObject(Object, String, Object) method on the DataServiceContext to create the item in the context along with the link to the related order. An HTTP POST message is sent to the data service when the SaveChanges() method is called.

Dim productId = 25
Dim customerId = "ALFKI"

Dim newItem As Order_Details = Nothing

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

Try
    ' Get the specific product.
    Dim selectedProduct = (From product In context.Products _
                           Where product.ProductID = productId _
                           Select product).Single()

    ' Get the specific customer.
    Dim cust = (From customer In context.Customers.Expand("Orders") _
                Where customer.CustomerID = customerId _
                Select customer).Single()

    ' Get the first order. 
    Dim order = cust.Orders.FirstOrDefault()

    ' Create the a new order detail for the specific product.
    newItem = Order_Details.CreateOrder_Details( _
            order.OrderID, selectedProduct.ProductID, 10, 5, 0)

    ' Add the new item with a link to the related order.
    context.AddRelatedObject(order, "Order_Details", newItem)
    context.SetLink(newItem, "Orders", order)

    ' Since the item is now tracked by the context,
    ' set just the link to the related product.
    context.AddLink(selectedProduct, "Order_Details", newItem)
    context.SetLink(newItem, "Products", selectedProduct)

    ' Add the new order detail to the collection, and
    ' set the reference to the product.
    order.Order_Details.Add(newItem)
    newItem.Orders = order
    newItem.Products = selectedProduct

    ' Send the inserts to the data service.
    context.SaveChanges()
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)

    ' Handle any errors that may occur during insert, such as 
    ' a constraint violation.
Catch ex As DataServiceRequestException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)
int productId = 25;
string customerId = "ALFKI";

Order_Details newItem = null;

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

try
{
    // Get the specific product.
    var selectedProduct = (from product in context.Products
                           where product.ProductID == productId
                           select product).Single();

    // Get the specific customer.
    var cust = (from customer in context.Customers.Expand("Orders")
                where customer.CustomerID == customerId
                select customer).Single();

    // Get the first order. 
    Orders order = cust.Orders.FirstOrDefault();

    // Create the a new order detail for the specific product.
    newItem = Order_Details.CreateOrder_Details(
        order.OrderID, selectedProduct.ProductID, 10, 5, 0);

    // Add the new item with a link to the related order.
    context.AddRelatedObject(order, "Order_Details", newItem);
    context.SetLink(newItem, "Orders", order);

    // Since the item is now tracked by the context,
    // set just the link to the related product.
    context.AddLink(selectedProduct, "Order_Details", newItem);
    context.SetLink(newItem, "Products", selectedProduct);

    // Add the new order detail to the collection, and
    // set the reference to the product.
    order.Order_Details.Add(newItem);
    newItem.Orders = order;
    newItem.Products = selectedProduct;

    // Send the inserts to the data service.
    context.SaveChanges();
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}

// Handle any errors that may occur during insert, such as 
// a constraint violation.
catch (DataServiceRequestException ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}

The following example shows how to use the AddObject(String, Object) method to add an Order_Details object to a related Orders object with a reference to a specific Products object. The AddLink(Object, String, Object) and SetLink(Object, String, Object) methods define the relationships. In this example, the navigation properties on the Order_Details object are also explicitly set.

Dim productId = 25
Dim customerId = "ALFKI"

Dim newItem As Order_Details = Nothing

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

Try
    ' Get the specific product.
    Dim selectedProduct = (From product In context.Products _
                                           Where product.ProductID = productId _
                                           Select product).Single()

    ' Get the specific customer.
    Dim cust = (From customer In context.Customers.Expand("Orders") _
                Where customer.CustomerID = customerId _
                Select customer).Single()

    ' Get the first order. 
    Dim order = cust.Orders.FirstOrDefault()

    ' Create the a new order detail for the specific product.
    newItem = Order_Details.CreateOrder_Details( _
    order.OrderID, selectedProduct.ProductID, 10, 5, 0)

    ' Add the new order detail to the context.
    context.AddToOrder_Details(newItem)

    ' Add links for the one-to-many relationships.
    context.AddLink(order, "Order_Details", newItem)
    context.AddLink(selectedProduct, "Order_Details", newItem)

    ' Set reference links for the many-to-one relationships.
    context.SetLink(newItem, "Orders", order)
    context.SetLink(newItem, "Products", selectedProduct)

    ' Add the new order detail to the collection, and
    ' set the reference to the product.
    order.Order_Details.Add(newItem)
    newItem.Products = selectedProduct

    ' Send the inserts to the data service.
    context.SaveChanges()
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)

    ' Handle any errors that may occur during insert, such as 
    ' a constraint violation.
Catch ex As DataServiceRequestException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)
int productId = 25;
string customerId = "ALFKI";

Order_Details newItem = null;

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

try
{
    // Get the specific product.
    var selectedProduct = (from product in context.Products
                           where product.ProductID == productId
                           select product).Single();

    // Get the specific customer.
    var cust = (from customer in context.Customers.Expand("Orders")
                where customer.CustomerID == customerId
                select customer).Single();

    // Get the first order. 
    Orders order = cust.Orders.FirstOrDefault();

    // Create the a new order detail for the specific product.
    newItem = Order_Details.CreateOrder_Details(
        order.OrderID, selectedProduct.ProductID, 10, 5, 0);

    // Add the new order detail to the context.
    context.AddToOrder_Details(newItem);

    // Add links for the one-to-many relationships.
    context.AddLink(order, "Order_Details", newItem);
    context.AddLink(selectedProduct, "Order_Details", newItem);

    // Set reference links for the many-to-one relationships.
    context.SetLink(newItem, "Orders", order);
    context.SetLink(newItem, "Products", selectedProduct);

    // Add the new order detail to the collection, and
    // set the reference to the product.
    order.Order_Details.Add(newItem);
    newItem.Products = selectedProduct;

    // Send the inserts to the data service.
    context.SaveChanges();
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}

// Handle any errors that may occur during insert, such as 
// a constraint violation.
catch (DataServiceRequestException ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}

See Also

Other Resources

Using a Data Service in a .NET Framework Application (ADO.NET Data Services)

How to: Add, Modify, and Delete Entities (ADO.NET Data Services)