I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models:
public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } }
and
public class Address { public int AddressId { get; set; } public string Street { get; set; } public string StreetCont { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public virtual Race Race { get; set; } }
I get the following error when trying to insert a new Race:
Unable to determine the principal end of an association between the types 'rcommander.Models.Race' and 'rcommander.Models.Address'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
Shouldn't it recognize RaceId as the primary key of the Races table and AddressId as the FK to the Addresses table automatically? Am I missing something?
Thanks!
Entity framework supports three types of relationships, same as database: 1) One-to-One 2) One-to-Many, and 3) Many-to-Many. We have created an Entity Data Model for the SchoolDB database in the Create Entity Data Model chapter.
You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.
The problem here seems to be that EntityFramework can't recognize where the foreing key is, as you are holding cross references in both objects. Not being sure what you want to achieve, I may suggest something like this:
public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } } public class Address { public int AddressId { get; set; } public string Street { get; set; } public string StreetCont { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } }
Skipping reference to Race in second entity.
The problem here is 1:1 relation between Address and Race! You probably want to map it as 1:N so you need to modify address to:
public class Address { public int AddressId { get; set; } public string Street { get; set; } public string StreetCont { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public virtual ICollection<Race> Races { ... } }
If you want to use 1:1 then you can't use AddressId in Race but AddressId in Address must be foreign key of Race because entity framework can achive 1:1 only be "sharing" primary key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With