Example:
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int BillingAddressId { get; set; }
public Address BillingAddress { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
// one user one billing adress
modelBuilder.Entity<User>()
.HasRequired(a => a.BillingAddress)
.WithMany()
.HasForeignKey(u => u.BillingAddressId);
Note: this is not going to generate the unique constraint in the db on BillingAddressId field
Reference:
http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx
No comments:
Post a Comment