Repository navigation
Open
Milestone
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<object> CreatingInstance(MaterializationInterceptionData materializationData, InterceptionResult<object> 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
Activity
ajcvickers commentedon Feb 27, 2024
@AllanMichaelsen I am not able to reproduce this--see my code below. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
AllanMichaelsen commentedon Feb 27, 2024
When creating the demo project, the interceptor was hit. I tried again to dig into the difference between my project and the demo project and I did find the culprit.
I use AutoMapperin my project and to be more specific I use the method:
.ProjectTo<TOut>(mapper.ConfigurationProvider)as seen below.So when I request TOut to be of my entity type the Interceptor is hit, but when I request my mapped Dto object, it is not. It seems that AutoMapper does not utilize the "InitializedInstance" (or any) method in the IMaterializationInterceptor.
The question is now. Is there a solution to this or do I have to rethink my mapping setup (I hope not)
ajcvickers commentedon Feb 27, 2024
@AllanMichaelsen The materialization interceptor only kicks in for the materialization of entity type instances, not arbitrary projected types.
Note for team triage: it could be useful to have an interceptor for creation of non-entity types.
AllanMichaelsen commentedon Feb 27, 2024
Noted. I have made a temporary fix via my Automapper projection and await an update for an non-entity inceptor.
Wil such update be posted here in this thread? Both if it is put in and in case it is decided not implement the feature?
ajcvickers commentedon Feb 27, 2024
@AllanMichaelsen If we decide it is worthwhile, then the issue will go on the backlog and be prioritized accordingly. Realistically, it's unlikely to happen any time soon.
[-]When using a IMaterializationInterceptor, no methods in it is called[/-][+]Materialization interception for non-entity types[/+]