LuckyPennySoftware/AutoMapper · error · InvalidOperationException

You cannot include a type map into itself.{Environment.NewLi

Error message

You cannot include a type map into itself.{Environment.NewLine}Source type: {types.SourceType.FullName}{Environment.NewLine}Destination type: {types.DestinationType.FullName}

What it means

IncludeDerivedTypes and IncludeBaseTypes link maps along an inheritance chain so configuration is shared. CheckDifferent rejects the case where the supplied TypePair equals the current map's own Types (source==this source, dest==this dest), because including a map into itself is a no-op cycle. The message lists both type full names.

Source

Thrown at src/AutoMapper/TypeMap.cs:189

        }
        return properties.Where(memberName => !Profile.GlobalIgnores.Any(memberName.StartsWith)).ToArray();
        IEnumerable<MemberMap> MappedMembers() => MemberMaps.Where(pm => pm.IsMapped);
    }
    public PropertyMap FindOrCreatePropertyMapFor(MemberInfo destinationProperty, Type destinationPropertyType)
    {
        var propertyMap = GetPropertyMap(destinationProperty.Name);
        if (propertyMap == null)
        {
            propertyMap = new(destinationProperty, destinationPropertyType, this);
            AddPropertyMap(propertyMap);
        }
        return propertyMap;
    }
    private void CheckDifferent(TypePair types)
    {
        if (types == Types)
        {
            throw new InvalidOperationException($"You cannot include a type map into itself.{Environment.NewLine}Source type: {types.SourceType.FullName}{Environment.NewLine}Destination type: {types.DestinationType.FullName}");
        }
    }
    internal void IgnorePaths(MemberInfo destinationMember)
    {
        foreach (var pathMap in PathMaps)
        {
            if (pathMap.MemberPath.First == destinationMember)
            {
                pathMap.Ignored = true;
            }
        }
    }
    public bool HasDerivedTypesToInclude => IncludedDerivedTypes.Count > 0;
    public void Seal(IGlobalConfiguration configuration)
    {
        if (_sealed)
        {
            return;

View on GitHub (pinned to dfa6dd587c)

Solutions

  1. Pass a genuinely different TypePair: a base pair for IncludeBase or a derived pair for Include.
  2. Remove the redundant self-include entirely; it carries no meaning.
  3. If you want to reuse shared configuration, factor it into a separate base map and IncludeBase that instead.
  4. In generic code, guard against new TypePair(s,d).Equals(currentMap.Types) before calling Include/IncludeBase.

Example fix

// before
CreateMap<Base, BaseDto>();
CreateMap<Derived, DerivedDto>().Include<Derived, DerivedDto>(); // self-include -> throws

// after
CreateMap<Base, BaseDto>();
CreateMap<Derived, DerivedDto>().IncludeBase<Base, BaseDto>(); // link to a different map
Defensive patterns

Strategy: validation

Validate before calling

TypePair current = new(typeof(TSrc), typeof(TDst));
TypePair provided = new(typeof(TInclude), typeof(TIncludeDest));
if (provided.Equals(current))
{
    // self-include is invalid; skip or throw a clearer domain error
    throw new InvalidOperationException("Cannot include a type map into itself.");
}

cfg.CreateMap<TSrc, TDst>().Include<TInclude, TIncludeDest>();

Try / catch

try { cfg.CreateMap<A, B>().Include<A, B>(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("cannot include a type map into itself"))
{
    // remove the self-include / supply a different TypePair
}

Prevention

When it happens

Trigger: Calling Include<S,D> or IncludeBase<S,D> on the very S->D map with the same type arguments, e.g. CreateMap<A,B>().Include<A,B>(), or generic/reflective configuration code that resolves derived/base args to the identical TypePair as the current map.

Common situations: Generic helpers that loop over type pairs and feed the map's own pair into Include; misunderstanding Include as 'apply this map's config to itself'; copy-paste from a base map; auto-generated attribute-based includes that collide.

Related errors


AI-assisted analysis of LuckyPennySoftware/AutoMapper@dfa6dd587c (2026-08-13). Data as JSON: /api/errors/3349387842f01bf5. Report an issue: GitHub.