0

I am very new to the Entity Framework, having decided that once and for all I am going to do a project using it and get some practice. I have a question regarding POCO object relationships for CodeFirst development.

The examples I have seen would layout an object like this:

public class Fruit {
     public int FruitId { get; set; }
     public string FruitName { get; set; }
}

public class Basket {
     public int BasketId { get; set; }
     public int FruitId { get; set; }
     public Fruit Contents { get; set; }
}

My question is, in the basket class, why define a FruitId and a Fruit Object? Wouldn't the existence of the Fruit Object imply the Id value?

Peter Lange
  • 2,509
  • 1
  • 22
  • 36
  • 1
    `FruitId` is the FK in the database schema and `Fruit` is the [navigation property](https://msdn.microsoft.com/en-us/data/jj713564.aspx) to the related object but doesn't show up on the DB. – Jasen Jun 02 '15 at 02:27
  • There's a bunch of information out there about why: http://stackoverflow.com/questions/14915938/in-what-scenarios-do-i-need-foreign-keys-and-navigation-properties-in-entity-fra – jjj Jun 02 '15 at 06:55
  • http://stackoverflow.com/questions/9253234/what-is-the-point-of-creating-foreign-key-properties-when-using-entity-framework – jjj Jun 02 '15 at 06:56
  • http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations – jjj Jun 02 '15 at 06:56
  • Also your Contents suggest plural Fruits, but you only have a singel entity. A basket should contain more than one fruit I guess, so it should have been List Contents?? That requires different mapping schemes. – jonas Jun 03 '15 at 06:35

0 Answers0