From 73fa006869ee0bbe9ffe003e78d3e2d84eca1cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Mon, 8 Sep 2025 16:01:03 +0300 Subject: [PATCH] Fix EF Core change tracking issue by creating a new dictionary instance during mapping --- .../MapperlyAutoObjectMappingProvider.cs | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs b/framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs index 71b3661303..c2b830b22f 100644 --- a/framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs +++ b/framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs @@ -236,13 +236,34 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider protected virtual void TryMapExtraProperties(MapExtraPropertiesAttribute? mapExtraPropertiesAttribute, TSource source, TDestination destination, ExtraPropertyDictionary destinationExtraProperty) { - if (mapExtraPropertiesAttribute != null && - typeof(IHasExtraProperties).IsAssignableFrom(typeof(TDestination)) && - typeof(IHasExtraProperties).IsAssignableFrom(typeof(TSource))) + if(source is not IHasExtraProperties sourceHasExtraProperties) + { + return; + } + + if (destination is not IHasExtraProperties destinationHasExtraProperties) + { + return; + } + + if (sourceHasExtraProperties.ExtraProperties == + destinationHasExtraProperties.ExtraProperties) + { + try + { + ObjectHelper.TrySetProperty(destinationHasExtraProperties, x => x.ExtraProperties, () => new ExtraPropertyDictionary(sourceHasExtraProperties.ExtraProperties)); + } + catch + { + // Has no setter, ignore it. + } + } + + if (mapExtraPropertiesAttribute != null) { MapExtraProperties( - source!.As(), - destination!.As(), + sourceHasExtraProperties, + destinationHasExtraProperties, destinationExtraProperty, mapExtraPropertiesAttribute.DefinitionChecks, mapExtraPropertiesAttribute.IgnoredProperties,