I'm having the following models i'm using in my app with Entity Framework 6 code first.
public class Customer
{
public Customer
{
}
public int Id { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public Address
{
}
public int Id { get; set; }
public string Street { get; set; }
public int Number { get; set; }
public int Country { get; set; }
public virtual Customer Customer { get; set; }
}
When i try to save them I get the following error:
Unable to determine the principal end of an association between the types Customer and Address
You need to speicify the foreign key relationships. As mentioned here , try adding a [ForeignKey("CustomerId")]
over public virtual Customer Customer { get; set; }
public virtual Customer Customer { get; set; }
public virtual Customer Customer { get; set; }
and [ForeignKey("AddressId")]
over public virtual Address Address { get; set; }
public virtual Address Address { get; set; }
public virtual Address Address { get; set; }
, and add those id fields to the model. Such As:
public class Customer
{
public Customer
{
}
public int Id { get; set; }
public string Name { get; set; }
public int Addressid { get; set; }
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
}
public class Address
{
public Address
{
}
public int Id { get; set; }
public string Street { get; set; }
public int Number { get; set; }
public int Country { get; set; }
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.