One-to-One Association

One-to-one associations are the simplest kind of associations. They usually correspond to the foreign key relations when the foreign key column is the primary key column.

The following two cases are possible, when we map a one-to-one association.

Case 1. When the foreign key and the primary key in the dependent entity class (the class that corresponds to the table that has the foreign key) consist of the same columns.

In this example a zero-or-one to one type association is created, which means that the class has a corresponding table that does not contain a foreign key to another table, and can have no reference to the related class. If you are sure, that every record in every table has a corresponding record in the related table, you can set the one-to-one type for the association in the conceptual model. If both tables have a foreign key reference to each other, then when adding such tables, the association between them will have the one-to-one type.

The database contains the Person and Contact tables, between which there is a foreign key created against the primary key fields of these tables.

images_Association1to1-table

We perform the following sequence of operations:

create an Entity Framework model;
add the Person and Contact tables to the model.

As a result, we have the following model:

images_Association1to1-model-EF

The association between the Person and the Contact entities looks as follows:

images_Association-Editor-1To1-EF

Case 2. When the foreign key is some non-primary unique property. You have to specify the foreign key column of the constrained table in this case. For the purpose of this example we use the following DDL scripts:

CREATE TABLE [dbo].[Company](
 [Company_ID] [int] NOT NULL,
 CONSTRAINT [PK_Company] PRIMARY KEY ([Company_ID]))
GO
CREATE TABLE [dbo].[Mailbox](
 [Mailbox_ID] [int] NOT NULL,
 [Company_ID] [int] NULL,
 CONSTRAINT [PK_Mailbox] PRIMARY KEY ([Mailbox_ID]))
GO
ALTER TABLE [dbo].[Mailbox]  WITH CHECK ADD  CONSTRAINT [FK_Mailbox_Company] FOREIGN KEY([Company_ID])
REFERENCES [dbo].[Company] ([Company_ID])
GO

 

The following tables are created:

images_Association1to1-nonprimary-table

We perform the following sequence of operations:

create an Entity Framework model;
add the Company and Mailbox tables to the model.

As a result, we have the following model:

 

images_Association1to1-nonprimary-model-EF

Now we delete the CompanyID foreign key property from the Mailbox entity in the design area.

Then we right-click the association and select Mapping Details from the context menu. The Association Mapping Details window is displayed:

images_Association1to1-nonprimary-mapping-details-EF

In the Association Mapping Details window, click the Mapped Entity drop-down list and select the Mailbox entity, in the Conditions area select Company_ID in the Column drop-down list, set the Operator value for it to IsNotNull add click OK.

Select the association between the Company and Mailbox entities, in the Properties window for the association locate the End 2 property, which currently has the value of Mailboxes (*), expand the End 2 property, locate the Multiplicity property and change its value from * (Many) to 1 (One) using its drop-down list, the End 2 property value automatically changes to Mailbox (1).

The final model is as follows:

images_Association1to1-nonprimary-model-final-EF

For the one-to-one association there is no need to set the "Company_ID IsNotNull" condition, as the column itself is not nullable in this case.

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.