How to: Manage One-to-One Associations
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.
Suppose you have the following domain model that includes two entities Orders and OrderDetails connected through a one-to-one association. The OrderDetails entity has exactly one instance of Orders. You can use the OrderDetails.Orders property to access the Orders instance that is associated with an instance of the OrderDetails entity. The Orders entity may have only one instance of OrderDetails. You can use the Orders.OrderDetails property to access the OrderDetails instances that is associated with the instance of the Orders entity.
In this scenario, by default the value of IsManaged for both navigation properties is True and additionally, the property itself is disabled. That allows you to access and modify the related objects without any concerns about introducing changes in the database that are not compatible with the relational model.
IsManaged notifies Telerik Data Access whether to populate the foreign key column of the child table in the database with the appropriate value when a new child object is added to the collection navigation property of a parent object. Since OrderDetails.Orders and Orders.OrderDetails are not collections, IsManaged is disabled.
Below you can find how to insert and delete objects from domain entities related through a one-to-one association:
Insert Related Objects
In case you need to insert a new order with the appropriate details, you can do it like this:
using (EntitiesModel dbContext = new EntitiesModel())
{
Orders newOrder = new Orders()
{
Id = 1,
DateCreated = DateTime.Now
};
OrderDetails newOrderDetails = new OrderDetails()
{
Customer = "Customer Name",
Employee = "Employee Name",
Product = "Product Name"
};
dbContext.Add(newOrder);
newOrder.OrderDetails = newOrderDetails;
dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
Dim newOrder As New Orders() With
{
.Id = 1,
.DateCreated = Date.Now
}
Dim newOrderDetails As New OrderDetails() With
{
.Customer = "Customer Name",
.Employee = "Employee Name",
.Product = "Product Name"
}
dbContext.Add(newOrder)
newOrder.OrderDetails = newOrderDetails
dbContext.SaveChanges()
End Using
Note that in this case only the newOrder is added to the context and newOrderDetails is assigned to the navigation property. After the call to the SaveChanges() method both objects are saved in the database keeping the relationship between them. This is called persistence by reachability, which means that if you build a graph of related objects, adding the top most one to the context will cause the context to track the rest too.
For general information about inserting objects you can check the How to: Insert Objects article.
Delete Related Objects
In case you need to delete an order from Orders (the parent table), you need to make sure that the corresponding object in OrderDetails is removed first. For example:
using (EntitiesModel dbContext = new EntitiesModel())
{
Orders someOrder = dbContext.Orders.FirstOrDefault();
dbContext.Delete(someOrder.OrderDetails);
dbContext.Delete(someOrder);
dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
Dim someOrder As Orders = dbContext.Orders.FirstOrDefault()
dbContext.Delete(someOrder.OrderDetails)
dbContext.Delete(someOrder)
dbContext.SaveChanges()
End Using
If the order details are not removed prior to the removal of the order you will experience DataStoreException: The DELETE statement conflicted with the REFERENCE constraint.
In case you need to remove only the details about a given order, you can pass someOrder.OrderDetails to the Delete() method of the context and then call SaveChanges().
For general information about deleting objects you can check the How to: Delete Objects article.