1:1 Relations and Entity Framework
Yesterday I tried to declare a 1:1 Relation in Entity Framework. As I have to write the EDMX File by hand (for various reasons) I’ve tried a straight forward way and ran into some problems. But first let me show you the
sample Model:

What I tried in the EDMX File (or better: CSDL, MSL and SSDL):
CSDL:
<association Name="FK_ParkingSpace_Employee">
<end Role="Employee" Type="Database1Model.Employee" Multiplicity="0..1" />
<end Role="ParkingSpace" Type="Database1Model.ParkingSpace" Multiplicity="0..1" />
</association>
Same in the SSDL:
<association Name="FK_ParkingSpace_Employee">
<end Role="Employee" Type="Database1Model.Store.Employee" Multiplicity="0..1" />
<end Role="ParkingSpace" Type="Database1Model.Store.ParkingSpace" Multiplicity="0..1" />
<referentialConstraint>
<principal Role="Employee">
<propertyRef Name="ID" />
</principal>
<dependent Role="ParkingSpace">
<propertyRef Name="fk_Employee" />
</dependent>
</referentialConstraint>
</association>
But that gave me this Error:
Multiplicity is not valid in Role ‘ParkingSpace’ in relationship ‘FK_ParkingSpace_Employee’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
What? Why? OK, I didn’t understand and could not find any information on the web how to define a 1:1 Relation in Entity Framework. So I made a small test application. And the SSDL was:
<association Name="FK_ParkingSpace_Employee">
<end Role="Employee" Type="Database1Model.Store.Employee" Multiplicity="0..1" />
<end Role="ParkingSpace" Type="Database1Model.Store.ParkingSpace" Multiplicity="*" />
<referentialConstraint>
<principal Role="Employee">
<propertyRef Name="ID" />
</principal>
<dependent Role="ParkingSpace">
<propertyRef Name="fk_Employee" />
</dependent>
</referentialConstraint>
</association>
Multiplicity=”*”
Ah, OK – from the Database’s viewpoint there is no 1:1 Relation. There’s a 1:n Relation. So I have to define 1:n Relation in the SSDL too. A Constraint in the Database and in the Object Model ensures, that this 1:n Relation is a 1:1 Relation. Or am I wrong?