title: Entity Framework with foreign keys
body: <p>In short how is Entity Framework suppose to work with related tables.</p>
  
  <p>aspnetcore 2.0</p>
  
  <pre><code>public class Table1{
      [Key]
      public int Id{get;set;}
  
      [ForeignKey("Table2")]
      public int Table2Id {get;set;}
      public virtual Table2 Table2{get;set;}
  }
  
  public class Table2{
      [Key]
      public int Id{get;set;}
  
  }
  </code></pre>
  
  <p>I assumed that with the statement </p>
  
  <pre><code>var t1 = context.Table1List.FirstOrDefault( j =&gt; j.Id == 1)
  </code></pre>
  
  <p>would have automatically populated Table2 but t1.Table2 is null.</p>
  
  <p>If I was to call context.Table2List.FirstOrDe..... then t1.Table2 is populated even without setting the property.  So EF recognizes the relationship just does not populate until I physically make the call to the DB.</p>
  
  <p>Is my understanding of how EF is suppose to work wrong or is just an error in my code.  Maybe something to do with lazyloading.</p>
  
  <p>I have read and read and read Microsofts tutorials but with no much understanding how it is Suppose to work versus how it actually works.</p>
  
  <p>Table 1 and 2 only have a one to one relationship with each other.</p>