title: Related data not loading with Entity Framework Core 2
body: <p>Facing a strange issue with EF Core 2.</p>
  
  <p>My Contact model:</p>
  
  <pre><code>    public class Contact
  {
      public int Id { get; set; }
      public int ExternalCustomerKey { get; set; }
      public string Firstname { get; set; }
      public string Surname { get; set; }
      public DateTime DateOfBirth { get; set; }
      public string Postcode { get; set; }
      public DateTime WhenCreated { get;  set; }
      public List&lt;ContactPreference&gt; Preferences { get; set; }
  }
  </code></pre>
  
  <p>ContactPreferene Model:</p>
  
  <pre><code> public class ContactPreference
  {
      public int  Id { get; set; }
      public int  PreferenceTypeId { get; set; }        
      public DateTime WhenCreated { get; set; }
      public DateTime LastModified { get; set; }        
      public int CustomerId { get; set; }
      public Contact Contact { get; set; }
  }
  </code></pre>
  
  <p>Retrieving contact from database:</p>
  
  <pre><code>       using (var context = new PreferenceServiceContext(_options))
          {
             var repository = new ContactRepository(context);
             var contact =  repository.FindContact(1);
          }
  </code></pre>
  
  <p>Contact is returned with preferences as null:</p>
  
  <p><a href="https://i.stack.imgur.com/pT44f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pT44f.png" alt="enter image description here"></a></p>
  
  <p>Inspecting the database context the preferences are being loaded:</p>
  
  <p><a href="https://i.stack.imgur.com/AXoIg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AXoIg.png" alt="enter image description here"></a></p>
  
  <p>And then the preferences are being loaded in the contact:</p>
  
  <p><a href="https://i.stack.imgur.com/qDj1Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qDj1Y.png" alt="enter image description here"></a></p>
  
  <p>If I don't inspect the context the preferences will be null. Anyone come across this or any suggestion please. Thanks</p>
  
  <p>Update:</p>
  
  <p><a href="https://i.stack.imgur.com/LRcW1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LRcW1.png" alt="enter image description here"></a></p>
  
  <p>Update 1:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          // -- Map relationships 
          modelBuilder.Entity&lt;Contact&gt;(entity =&gt;
          {
              entity.ToTable("Contact", "Preference");
              entity.HasKey(e =&gt; e.Id);
              entity.Property(e =&gt; e.ExternalCustomerKey).HasColumnName("ExternalCustomerKey");
              entity.Property(e =&gt; e.Firstname).HasColumnName("Firstname");
              entity.Property(e =&gt; e.Surname).HasColumnName("Surname");
              entity.Property(e =&gt; e.DateOfBirth).HasColumnName("DateOfBirth");
              entity.Property(e =&gt; e.Postcode).HasColumnName("Postcode");
              entity.Property(e =&gt; e.WhenCreated).HasColumnName("WhenCreated");
              entity.HasMany(e =&gt; e.Preferences).WithOne(e =&gt; e.Contact);
          });
  
          modelBuilder.Entity&lt;ContactPreference&gt;(entity =&gt;
          {
              entity.ToTable("ContactPreference", "Preference");
              entity.HasKey(e =&gt; e.Id);
              entity.Property(e =&gt; e.CustomerId).HasColumnName("CustomerId");
              entity.Property(e =&gt; e.PreferenceTypeId).HasColumnName("PreferenceTypeId");                
              entity.Property(e =&gt; e.WhenCreated).HasColumnName("WhenCreated");
              entity.Property(e =&gt; e.LastModified).HasColumnName("LastModified");
              entity.Property(e =&gt; e.AgentId).HasColumnName("AgentId");
              entity.HasOne(e =&gt; e.Contact).WithMany(e =&gt; e.Preferences);
          });
  </code></pre>