jahan
jahan

Reputation: 124

Related data not loading with Entity Framework Core 2

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

Upvotes: 1

Views: 3855

Answers (2)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89396

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://learn.microsoft.com/en-us/ef/core/querying/tracking

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

Upvotes: 4

Lajos Arpad
Lajos Arpad

Reputation: 77012

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.

Upvotes: 0

Related Questions