0

For the Entity below

class Entity
{
   public AnotherEntity AT { get; set; }
}

Many times I see people building model in this way

class Entity
{
   public AnotherEntity AT { get; set; }

   public int AT_Id { get; set; }
}

What is the use of this foreign key property? I can even see that Julie Lehrman recommend going this way. Why don't I just use Entity.AT.Id to access and update the model.

Steve Greene
  • 11,089
  • 1
  • 27
  • 45
Sisyphus
  • 803
  • 7
  • 26
  • 1
    Because in the DB the table will have the foreign key, not the entire object and you might only want data from that table and not the other one, including the foreign key. – juharr Sep 26 '17 at 18:12
  • Below link has description why foreign keys are important https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx – Bhargavi Sep 26 '17 at 18:19
  • 3
    Exposing a FK is optional. You can work with just navigation properties if desired (EF will create a FK in the database though). It can make certain updates easier where you know the value already exists. – Steve Greene Sep 26 '17 at 18:59
  • The idea here is called **Referential Integrity** – T.S. Sep 26 '17 at 19:41

0 Answers0