{"record":{"id":"3349387842f01bf5","repo":"LuckyPennySoftware/AutoMapper","slug":"you-cannot-include-a-type-map-into-itself-environ","errorCode":null,"errorMessage":"You cannot include a type map into itself.{Environment.NewLine}Source type: {types.SourceType.FullName}{Environment.NewLine}Destination type: {types.DestinationType.FullName}","messagePattern":"You cannot include a type map into itself\\.(.+?)Source type: (.+?)(.+?)Destination type: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/TypeMap.cs","lineNumber":189,"sourceCode":"        }\n        return properties.Where(memberName => !Profile.GlobalIgnores.Any(memberName.StartsWith)).ToArray();\n        IEnumerable<MemberMap> MappedMembers() => MemberMaps.Where(pm => pm.IsMapped);\n    }\n    public PropertyMap FindOrCreatePropertyMapFor(MemberInfo destinationProperty, Type destinationPropertyType)\n    {\n        var propertyMap = GetPropertyMap(destinationProperty.Name);\n        if (propertyMap == null)\n        {\n            propertyMap = new(destinationProperty, destinationPropertyType, this);\n            AddPropertyMap(propertyMap);\n        }\n        return propertyMap;\n    }\n    private void CheckDifferent(TypePair types)\n    {\n        if (types == Types)\n        {\n            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}\");\n        }\n    }\n    internal void IgnorePaths(MemberInfo destinationMember)\n    {\n        foreach (var pathMap in PathMaps)\n        {\n            if (pathMap.MemberPath.First == destinationMember)\n            {\n                pathMap.Ignored = true;\n            }\n        }\n    }\n    public bool HasDerivedTypesToInclude => IncludedDerivedTypes.Count > 0;\n    public void Seal(IGlobalConfiguration configuration)\n    {\n        if (_sealed)\n        {\n            return;","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/TypeMap.cs#L171-L207","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a genuinely different TypePair: a base pair for IncludeBase or a derived pair for Include.","Remove the redundant self-include entirely; it carries no meaning.","If you want to reuse shared configuration, factor it into a separate base map and IncludeBase that instead.","In generic code, guard against new TypePair(s,d).Equals(currentMap.Types) before calling Include/IncludeBase."],"exampleFix":"// before\nCreateMap<Base, BaseDto>();\nCreateMap<Derived, DerivedDto>().Include<Derived, DerivedDto>(); // self-include -> throws\n\n// after\nCreateMap<Base, BaseDto>();\nCreateMap<Derived, DerivedDto>().IncludeBase<Base, BaseDto>(); // link to a different map","handlingStrategy":"validation","validationCode":"TypePair current = new(typeof(TSrc), typeof(TDst));\nTypePair provided = new(typeof(TInclude), typeof(TIncludeDest));\nif (provided.Equals(current))\n{\n    // self-include is invalid; skip or throw a clearer domain error\n    throw new InvalidOperationException(\"Cannot include a type map into itself.\");\n}\n\ncfg.CreateMap<TSrc, TDst>().Include<TInclude, TIncludeDest>();","typeGuard":null,"tryCatchPattern":"try { cfg.CreateMap<A, B>().Include<A, B>(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"cannot include a type map into itself\"))\n{\n    // remove the self-include / supply a different TypePair\n}","preventionTips":["In generic include helpers, skip when the provided TypePair equals the map's own Types.","Use IncludeBase to point at a shared base map rather than re-including the current pair.","Review auto-generated/attribute includes for accidental self-references."],"tags":["configuration","inheritance","include","typemap"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}