.NET 4.0 with SQL Server 2008 R2. I am trying to represent a 0..1 to 0..1 relationship and I keep getting the following error:
Error 113: Multiplicity conflicts with the referential constraint in Role ‘{0}’ in relationship ‘{1}’. Because all of the properties in Dependent Role are non-nullable, multiplicity of the Principal Role must be ‘1’.
I get this message even when the properties in the dependent entity are nullable. Is it possible to represent this relationship in Entity Framework?
An example of when this might occur (independently of whether it is good database design to do so) is when you have a request that exists by itself, a result that becomes associated with it once the request is completed, and when the request is eventually pruned but the results all remain.
No it is not possible and I doubt it works in SQL server. Database relation requires that one end becomes dependent. It means that it references primary key (PK) of a principal end – we call this foreign key (FK). If we talk about one-to-one relation the FK must be marked as unique so that only one record in the dependent table can reference a given record from the principal table. The only valid relation in this case is 0..1 – 1 where principal can exist without dependent but the dependent can exist only when related to existing principal because its FK value must be set to PK value of the principal. Theoretically FK can be nullable but it depends on the way how database implements unique constraints. If the database counts null as another unique value only one dependent record can have FK set to null (I think this is a case of SQL server).
In EF this is even more complicated because EF doesn’t support unique constraints and because of that you can build one-to-one relation only when FK in dependent entity is also its PK (= no way to set it to null). If you cannot set FK to null you cannot have it nullable and because of that principal entity must exists otherwise the referential integrity will throw error.
The best solution for you is considering
Requestas principal entity andResultas dependent. Request must be created first and it must be kept in the database as long as theResult.Resultmust have same PK value (the column cannot be auto incremented) as the correspondingRequest(and PK must be FK toRequest).