dotnet / dotnet/efcore

Materialization interception for non-entity types

Open
#33,157 5 comments 0 reactions 0 assignees View on GitHub
area-interception customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I added an IMaterializationInterceptor like below (the exceptions are for this purpose only):
```
public class MaterializationInterceptor : IMaterializationInterceptor
{
private MaterializationInterceptor() { }

public object CreatedInstance(MaterializationInterceptionData materializationData, object entity)
{
throw new Exception("CreatedInstance");
}

public InterceptionResult CreatingInstance(MaterializationInterceptionData materializationData, InterceptionResult result)
{
throw new Exception("CreatingInstance");
}

public object InitializedInstance(MaterializationInterceptionData materializationData, object instance)
{
throw new Exception("InitializedInstance");
}

public InterceptionResult InitializingInstance(MaterializationInterceptionData materializationData, object entity, InterceptionResult result)
{
throw new Exception("InitializingInstance");
}

public static MaterializationInterceptor Instance { get; } = new();
}
```

Added to my contect as:
```
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);

optionsBuilder.AddInterceptors(
MaterializationInterceptor.Instance,
DbCommandInterceptor.Instance
);
}
```

When I run a query against my context I didn't get the expected result from the value substitution I have in the "InitializedInstance" method.
During debugging I can see that the constructor is hit, but my breakpoints are never hit. So for some reason the methods (any of the four) are never hit.

I also have a "DbCommandInterceptor" and a "SaveChangesInterceptor". Both of those work fine.

EF Core version: 8,0,2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11 Pro
IDE: Visual Studio 2022 17.9.0 Preview 4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.