diff --git a/AgileMapper/Configuration/UserConfigurationSet.cs b/AgileMapper/Configuration/UserConfigurationSet.cs index 3ca074145..d1bb91764 100644 --- a/AgileMapper/Configuration/UserConfigurationSet.cs +++ b/AgileMapper/Configuration/UserConfigurationSet.cs @@ -131,7 +131,14 @@ public void Add(EntityKeyMappingSetting setting) } public bool MapEntityKeys(IBasicMapperData basicData) - => _entityKeyMappingSettings?.FirstOrDefault(s => s.AppliesTo(basicData))?.MapKeys == true; + { + var applicableSetting = _entityKeyMappingSettings? + .FirstOrDefault(s => s.AppliesTo(basicData))? + .MapKeys; + + return (applicableSetting == true) || + (basicData.RuleSet.Settings.AllowEntityKeyMapping && (applicableSetting != false)); + } #endregion diff --git a/AgileMapper/MappingRuleSetCollection.cs b/AgileMapper/MappingRuleSetCollection.cs index 8e5baa4da..ab377af88 100644 --- a/AgileMapper/MappingRuleSetCollection.cs +++ b/AgileMapper/MappingRuleSetCollection.cs @@ -38,6 +38,7 @@ internal class MappingRuleSetCollection { UseMemberInitialisation = true, UseSingleRootMappingExpression = true, + AllowEntityKeyMapping = true, AllowCloneEntityKeyMapping = true, GuardAccessTo = value => value.Type.IsComplex(), ExpressionIsSupported = value => value.CanBeProjected(), diff --git a/AgileMapper/MappingRuleSetSettings.cs b/AgileMapper/MappingRuleSetSettings.cs index d21b3775f..b3acd85b3 100644 --- a/AgileMapper/MappingRuleSetSettings.cs +++ b/AgileMapper/MappingRuleSetSettings.cs @@ -44,6 +44,8 @@ public static MappingRuleSetSettings ForInMemoryMapping( public bool CheckDerivedSourceTypes { get; set; } + public bool AllowEntityKeyMapping { get; set; } + public bool AllowCloneEntityKeyMapping { get; set; } public Func GuardAccessTo { get; set; } diff --git a/AgileMapper/Members/MemberMapperDataExtensions.cs b/AgileMapper/Members/MemberMapperDataExtensions.cs index 3f5029e19..2ea9abd07 100644 --- a/AgileMapper/Members/MemberMapperDataExtensions.cs +++ b/AgileMapper/Members/MemberMapperDataExtensions.cs @@ -4,6 +4,11 @@ namespace AgileObjects.AgileMapper.Members using System.Collections.Generic; using System.Diagnostics; using System.Linq; +#if NET35 + using Microsoft.Scripting.Ast; +#else + using System.Linq.Expressions; +#endif using System.Reflection; using Configuration; using DataSources; @@ -12,12 +17,8 @@ namespace AgileObjects.AgileMapper.Members using Extensions.Internal; using NetStandardPolyfills; using ObjectPopulation; -#if NET35 - using Microsoft.Scripting.Ast; -#else - using System.Linq.Expressions; -#endif using static Member; + using static System.StringComparison; internal static class MemberMapperDataExtensions { @@ -26,7 +27,10 @@ public static bool TargetTypeIsEntity(this IMemberMapperData mapperData) public static bool IsEntity(this IMemberMapperData mapperData, Type type, out Member idMember) { - if (type == null) + if ((type == null) || + type.Name.EndsWith("ViewModel", Ordinal) || + type.Name.EndsWith("Dto", Ordinal) || + type.Name.EndsWith("DataTransferObject", Ordinal)) { idMember = null; return false; @@ -210,8 +214,9 @@ public static bool TargetMemberIsUnmappable( // If we're here: // 1. TargetMember is an Entity key - // 2. No configuration exists to allow Entity key Mapping - // 3. No configured data sources exist + // 2. The rule set doesn't allow entity key mapping + // 3. No configuration exists to allow Entity key Mapping + // 4. No configured data sources exist if (mapperData.RuleSet.Settings.AllowCloneEntityKeyMapping && (mapperData.SourceType == mapperData.TargetType))