0

I'm kind of confuse with the navigation properties in the EF Code First.

The following 2 classes come from ScottGu tutorial on EF Code First.

public class Dinner
{
  public int DinnerID { get; set; }
  public string Title { get; set; }
  public DateTime EventDate { get; set; }
  public string Address { get; set; }
  public string HostedBy { get; set; }   

  public virtual ICollection<RSVP> RSVPs { get; set; }   
}

public class RSVP
{
  public int RsvpID { get; set; }
  public int DinnerID { get; set; }
  public string AttendeeMail { get; set; }

  public virtual Dinner Dinner { get; set; }
}

They are 2 tables In the ScottGu's NerdDinner tutorial: Dinners and RSVP. The column DinnerID is the primary-key for Dinner and a foreign-key for RSVP.

Those are my questions:

  1. I understand why there's a ICollection RSVPs property in the Diner class, but why a Dinner Dinner property in the RSVP class?
  2. What's going to happen if I do not add the DINER navigation property in the RSVP class?

I can sum up all my questions by saying: "Isn't there any relationship between Foreign-key in the underline tables and the navigation properties in the model?"

Navigation properties are there to reflect the relationship between tables in the underline database or not?

I'm sorry to be so lengthy, I'm really confused.

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252
  • 1
    Also check this: http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations/5282275#5282275 – Ladislav Mrnka Aug 20 '11 at 15:35

1 Answers1

2
  1. ICollection RSVPs property in the Diner class and Dinner property in the RSVP class are the navigation properties which indicating that there is a one to many relationship between Diner and RSVP.Yes ,navigation properties are there to reflect the reationship.

    2.You can do like that. you should have a navigation property for at least one side to map the relationsip. EF4 Code First: how to add a relationship without adding a navigation property

Community
  • 1
  • 1
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92