Data Access has been discontinued. Please refer to this page for more information.

How to: Create 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.

One-to-One relationships are frequently found in database models. The One-to-One relationships can be created in three ways:

  • Foreign Key Constraint pointing from the Primary Key column(s) of the source to the Primary Key column(s) of the target table.
  • Foreign Key Constraint pointing from a Unique Column(s) of a source table to the Primary Key Column(s) of the target table.
  • Foreign Key Constraint pointing from a Unique Column(s) of a source table to respective Unique Column(s) of the target table.

The first scenario is the most common one, and the one Telerik Data Access currently supports.

Telerik Data Access supports only one-to-one associations defined as a foreign key constraint pointing from the primary key column of the source table to the primary key column of the target table.

There are two ways to represent one-to-one associations in the Visual Designer: creating vertical inheritance or using one-to-one relations. This topic is focused on the second approach:

Database First Mapping

Suppose, you have Customers and CustomerDetails tables in the database, connected via a foreign key constraint which defines an one-to-one relation between the primary keys of the respective tables.

From object-oriented point of view, it is not appropriate to create an inheritance relationship where Customer is the base class and CustomerDetails is the derived class. With the one-to-one support, having a PK->PK constraint in the database, is automatically mapped to an association in the visual designer where one of the ends has a multiplicity of 1 and the other 0..1.

Model First Mapping

One-to-one associations can be created in model-first scenario as well. For this purpose, you use the Association Editor and the workflow is the same as defining any normal one-to-many association. The only detail here is that, when the members(columns) selected for the source and target entity are Primary Keys, the created association is automatically created as one-to-one instead of one-to-many.

A 1-1 association is created by matching the primary keys from the source and the target class in the Association Editor dialog.

Using One-To-One Associations Runtime

Since 1-1 associations are based on the primary keys, Telerik Data Access needs to determine which domain class specifies the primary key to be saved in the database. Assuming the user specified a key for the Customer class and for the CustomerDetail class. Which setting will be stored in the database? This is calculated based on the settings of the respective classes. If there is a key generator involved, then the primary key generated for this class is used for the primary key of the referenced class as well. If no key generator is specified, then the class that has a key specified will propagate the key for the associated class as well. If both classes have specified a primary key, and the keys don’t match, then the primary key is taken from the class that was referenced. Telerik Data Access will synchronize the primary keys based on these rules in order to store the 1-1 associations correctly.

In the following example, the primary key property for the Customer entity is set to 1 and the primary key property for the CustomerDetail entity is set to 2. However, when you invoke SaveChanges, Telerik Data Access will propagate the CustomerDetail's key for the associated Customer, i.e. the primary key for the new row in the Customer table will be 2.

using ( EntitiesModel dbContext = new EntitiesModel() )
   Customer customer = new Customer
       CustomerId = 1,
       Name = "John",
       Type = "1"
   CustomerDetail customerDetail = new CustomerDetail
       CustDetailId = 2,
       Address = "USA",
       Phone = "111"
   customer.CustomerDetail = customerDetail;
Using dbContext As New EntitiesModel()
 Dim _customer As Customer = New Customer _
    With {.CustomerId = 1, .Name = "John", .Type = "1"}
 Dim _customerDetail As CustomerDetail = New CustomerDetail _
    With {.CustDetailId = 2, .Address = "USA", .Phone = "111"}
 _customer.CustomerDetail = _customerDetail
End Using