Related data not loading with Entity Framework Core 2

asp.net-core entity-framework entity-framework-core

Question

Facing a strange issue with EF Core 2.

My Contact model:

    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<ContactPreference> Preferences { get; set; }
}

ContactPreferene Model:

 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; }
}

Retrieving contact from database:

       using (var context = new PreferenceServiceContext(_options))
        {
           var repository = new ContactRepository(context);
           var contact =  repository.FindContact(1);
        }

Contact is returned with preferences as null:

enter image description here

Inspecting the database context the preferences are being loaded:

enter image description here

And then the preferences are being loaded in the contact:

enter image description here

If I don't inspect the context the preferences will be null. Anyone come across this or any suggestion please. Thanks

Update:

enter image description here

Update 1:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // -- Map relationships 
        modelBuilder.Entity<Contact>(entity =>
        {
            entity.ToTable("Contact", "Preference");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ExternalCustomerKey).HasColumnName("ExternalCustomerKey");
            entity.Property(e => e.Firstname).HasColumnName("Firstname");
            entity.Property(e => e.Surname).HasColumnName("Surname");
            entity.Property(e => e.DateOfBirth).HasColumnName("DateOfBirth");
            entity.Property(e => e.Postcode).HasColumnName("Postcode");
            entity.Property(e => e.WhenCreated).HasColumnName("WhenCreated");
            entity.HasMany(e => e.Preferences).WithOne(e => e.Contact);
        });

        modelBuilder.Entity<ContactPreference>(entity =>
        {
            entity.ToTable("ContactPreference", "Preference");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.CustomerId).HasColumnName("CustomerId");
            entity.Property(e => e.PreferenceTypeId).HasColumnName("PreferenceTypeId");                
            entity.Property(e => e.WhenCreated).HasColumnName("WhenCreated");
            entity.Property(e => e.LastModified).HasColumnName("LastModified");
            entity.Property(e => e.AgentId).HasColumnName("AgentId");
            entity.HasOne(e => e.Contact).WithMany(e => e.Preferences);
        });
1
1
9/20/2017 12:33:14 PM

Accepted Answer

Inspecting the database context the preferences are being loaded:

Enumerating the context.ContactPreferences DbSet causes them to be fetched from the database into the DbContext cache. When you later retrieve a Contact, EF "fixes up" the Preferences navigation propertiy so it contains the already-fetched ContactPreference objects. See https://docs.microsoft.com/en-us/ef/core/querying/tracking

Related entities are not loaded unless you request it. Here's how to request it: https://docs.microsoft.com/en-us/ef/core/querying/related-data

4
9/20/2017 11:11:54 AM

Popular Answer

You have a ContactPreferences member somewhere, which is different from yourContact.Preferences. You will need to use yourContact.ContactPreferences instead of yourContact.Preferences if ContactPreferences is a member of Contact.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow