Entity Framework One-To-One Mapping Issues

Using VS 2010 beta 2, ASP.NET MVC.

I tried to create an Entity framework file and got the data from my database.

There were some issues with the relationships, so I started to tweak things around, but I kept getting the following error for simple one-to-one relationships

Error 1 Error 113: Multiplicity is not valid in Role 'UserProfile' in relationship 'FK_UserProfiles_Users'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. myEntities.edmx 2024

My Users table is consists of some other many-to-many relationships to other tables, but when I try to make a one-to-one relationship with other tables, that error pops up.

Users Table

  • UserID
  • Username
  • Email

etc..

UserProfiles Table

  • UserProfileID
  • UserID (FK for Users Table)
  • Location
  • Birthday

Solution 1:

For one-to-one relationships, EF expects that the tables are using the same primary key. And really, if it's a true one-to-one they probably should. So in your example, if you make UserID the primary key on the UserProfiles table, your one-to-one will work.

Solution 2:

I have a similar issue, but with a sale and layby scenario.

A layby can exist without a sale, and a sale can exist without a layby. This means I have a 0 or 1 to 0 or 1 relationship.

Layby references sale, but layby cannot use the primary key of Sale, and Sale cannot use the primary key of Layby.

I solved the problem by using a 0 or 1 to many relationship, configured the 'Laybys' getter and setter on the sale as private, and then provided my own 'Layby' getter and setter in my POCO.