views:

44

answers:

2

Hello all,

I'm sure I'm missing something very simple, but let's say I have two entities, Employee and EmployeeType.

Employee type would contain values like 'Full time', 'Contractor', 'Intern', etc.

An Employee entity would contain one, and only one EmployeeType value.

So I am designing a new .edmx model using the Model-First approach and generating my actual sql server data schema from the model.

I want to add an integer type foreign key id into my Employee entity, EmployeeTypeId, which will map to the primary key of the EmployeeType entity.

So I've gone ahead and done that in my Employee entity. Where I'm stuck is how, though the Entity Framework designer, to enforce the 1:1 referential constraint on that EmployeeTypeId property? Or does the EF handle that automatically behind the scenes?

thanks in advance, John

A: 

You first create a new association (if you haven't done this already) between the two entities. Just right-click on the edmx designer and choose Add -> Association.

When you click on the association you have just created in the model designer, in the properties window, you can set the End1 Multiplicity and End2 Multiplicity properties to 1. This will ensure that you can set only one relation entity while using the entity framework. This does not get enforced in SQL server by the way, because SQL server does not implicitly support 1:1 relationships.

Jappie
+1  A: 

Think I figured out the answer to my own question. In the EF .edmx surface designer, I needed to right click on the scalar property I wanted to set as a foreign key id to the other entity and choose 'Entity Key'.

Once that was done, I could go into the referential constraints dialog box and point my new foreign key property to the other entity.

If you don't explicitly set your foreign key property as 'Entity Key', EF will think you want to point your primary key id to the other table.

cheers

John K.