Skip to content

IMaterializationInterceptor on InitializedInstance doesn't have loaded included properties (child entities) #32535

@Kasta11111

Description

@Kasta11111

File a bug

When the InitializedInstance of IMaterializationInterceptor is called, only properties that belong to the current object are loaded, child entities (objects) fetched with Include() are not. On the position where I fetch the record from the database is the object loaded correctly with its childs.

Include your code

public interface IMaterializable
{
    void OnMaterialized();
}

public class MaterializedInterceptor : IMaterializationInterceptor
{
    public object InitializedInstance(MaterializationInterceptionData materializationData, object instance)
    {
        if (instance is IMaterializable materializable)
        {
            materializable.OnMaterialized();
        }

        return instance;
    }
}

public class MyDbContext : IdentityDbContext
{
    private static readonly MaterializedInterceptor _materializedInterceptor = new();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    { 
        optionsBuilder.AddInterceptors(_materializedInterceptor);
    }
}

public class Father() : IMaterializable
{
   public int Id { get; set; }
   public string Name { get; set; }
   public Daughter Daughter { get; set; }
   public List<Son> Sons { get; set; }

   public void OnMaterialized()
   {
        // Id and Name are loaded
        // Daughter and Sons are not loaded
   }
}

public class Son() : IMaterializable
{
    public int Id { get; set; }

    public string Name { get; set; }

    public void OnMaterialized()
    {
    }
}

public class Daughter() : IMaterializable
{
    public int Id { get; set; }

    public string Name { get; set; }

    public void OnMaterialized()
    {
    }
}

public void GetFathers()
{
    Father father = await _dbContext.Fathers.Include(b => b.Daughter).Include(i => i.Sons).FirstOrDefaultAsync();
    // father.Daughter and father.Sons are loaded
}

Include provider and version information

EF Core version: 7.0.14 and 8.0.0 (both)
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0.100
Operating system: Windows 10
IDE: Visual Studio 2022 17.9.0 Preview 1.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions