1

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your input. I updated my mappings and changes the query to include preferences rather then using Find and preferences seem to have come through.
0

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.

6 Comments

I have updated the question with ContactPreference class which has Contact as a member.
@jahan can you find the declaration of ContactPreferences? What class is it defined in?
Contact and ContactPreference are two separate classes. If the preferences didn't load at all then It would make sense. But if the contact object has been created with null preferences and then I inspect the context the preferences get updated in the contact model.
@jahan what relation is symbolized by Contact.Preferences? Can you give us context about your tables and relations?
see the update in the question please. The relation is one to many.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.