diff --git a/src/EFCore/Diagnostics/MaterializationInterceptionData.cs b/src/EFCore/Diagnostics/MaterializationInterceptionData.cs index 0532852e330..73cf44c1617 100644 --- a/src/EFCore/Diagnostics/MaterializationInterceptionData.cs +++ b/src/EFCore/Diagnostics/MaterializationInterceptionData.cs @@ -28,11 +28,13 @@ public readonly struct MaterializationInterceptionData public MaterializationInterceptionData( MaterializationContext materializationContext, IEntityType entityType, + QueryTrackingBehavior? queryTrackingBehavior, Dictionary Accessor)> valueAccessor) { _materializationContext = materializationContext; _valueAccessor = valueAccessor; EntityType = entityType; + QueryTrackingBehavior = queryTrackingBehavior; } /// @@ -46,6 +48,11 @@ public DbContext Context /// public IEntityType EntityType { get; } + /// + /// The query tracking behavior, or if this materialization is not from a query. + /// + public QueryTrackingBehavior? QueryTrackingBehavior { get; } + /// /// Gets the property value for the property with the given name. /// diff --git a/src/EFCore/Query/Internal/EntityMaterializerSource.cs b/src/EFCore/Query/Internal/EntityMaterializerSource.cs index 159707ece88..58fb8c61702 100644 --- a/src/EFCore/Query/Internal/EntityMaterializerSource.cs +++ b/src/EFCore/Query/Internal/EntityMaterializerSource.cs @@ -165,6 +165,7 @@ private static readonly ConstructorInfo MaterializationInterceptionDataConstruct { typeof(MaterializationContext), typeof(IEntityType), + typeof(QueryTrackingBehavior?), typeof(Dictionary)>) })!; @@ -242,6 +243,7 @@ private static Expression CreateInterceptionMaterializeExpression( MaterializationInterceptionDataConstructor, materializationContextExpression, Expression.Constant(entityType), + Expression.Constant(bindingInfo.QueryTrackingBehavior, typeof(QueryTrackingBehavior?)), accessorDictionaryVariable)), Expression.Assign( creatingResultVariable, diff --git a/test/EFCore.Specification.Tests/MaterializationInterceptionTestBase.cs b/test/EFCore.Specification.Tests/MaterializationInterceptionTestBase.cs index 21fc249b0fc..34f3e69c934 100644 --- a/test/EFCore.Specification.Tests/MaterializationInterceptionTestBase.cs +++ b/test/EFCore.Specification.Tests/MaterializationInterceptionTestBase.cs @@ -83,6 +83,7 @@ public virtual void Intercept_query_materialization_for_empty_constructor(bool i { Assert.Same(context, data.Context); Assert.Same(data.Context.Model.FindEntityType(typeof(Book)), data.EntityType); + Assert.Equal(QueryTrackingBehavior.TrackAll, data.QueryTrackingBehavior); var idProperty = data.EntityType.FindProperty(nameof(Book.Id))!; var id = data.GetPropertyValue(nameof(Book.Id))!; @@ -189,6 +190,7 @@ public virtual void Intercept_query_materialization_for_full_constructor(bool in { Assert.Same(context, data.Context); Assert.Same(data.Context.Model.FindEntityType(typeof(Pamphlet)), data.EntityType); + Assert.Equal(QueryTrackingBehavior.TrackAll, data.QueryTrackingBehavior); var idProperty = data.EntityType.FindProperty(nameof(Pamphlet.Id))!; var id = data.GetPropertyValue(nameof(Pamphlet.Id))!;