Using EF5 Code first, I have two classes:
[Table("UserProfile")]
public class UserProfile {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
}
[Table("Address")]
public class Address : IEntity {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public virtual State State { get; set; }
public string ZipCode { get; set; }
[ForeignKey("UserId")]
public virtual UserProfile User { get; set; }
}
UserProfile
should always have an Address
but I also wanted to have a navigation property in Address
so that I could look up users by address. So, once migrations has done its thing, I'd like the tables to look like...
UserProfile
UserId (PK)
...
Address
AddressId (PK)
...
UserId (FK)
From the Package Manager Console, I'm running update-database
and getting the following message...
Unable to determine the principal end of an association between the types 'TastySerpent.Domain.Models.Address' and 'TastySerpent.Domain.Models.UserProfile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I'm confused on how to set up one-to-one relationships in Entity Framework 5.
The trick with 1:1 in EF is the dependant table MUST have the same primary key. The Primary tables has the DB generated ID. The dependant uses DatabaseGeneratedOption.None !
so the fluentAPI for the dependant table
You define the principal when you map:
The dependent gets the foreign key that references the principal's key. When it's a one to one, that foreign key is also the dependent's primary key but EF can't work out which is which and that's why you get an error until you've specified it.
References:
http://msdn.microsoft.com/en-us/library/ee382827.aspx
https://stackoverflow.com/a/19580798/150342