Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 - Model Relationships

Tags:

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!

like image 568
Mike Avatar asked Mar 18 '11 12:03

Mike


People also ask

What kind of relationships are supported by Entity Framework?

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.

How will you create relationship between tables in Entity Framework?

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.


2 Answers

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.

like image 135
tpeczek Avatar answered Sep 22 '22 08:09

tpeczek


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.

like image 20
Ladislav Mrnka Avatar answered Sep 21 '22 08:09

Ladislav Mrnka