4

I'm using AutoMapper.Extensions.Microsoft.DependencyInjection Nuget package in my .net core 3.1 project. My console app code is below (can be run by copy paste and install AutoMapper.Extensions.Microsoft.DependencyInjection package for reproduce error).

I'm not writing any mapping. I'm using Automapper only for clone object like below code. When I use 6.1.0 package everything working perfectly. But when I upgrade 6.1.1 or 7.0.0 it gave me error

Missing type map configuration or unsupported mapping. Mapping types: Foo -> FooDto AutomapperProject.Foo -> AutomapperProject.FooDto.

What can be the reason of this?

using AutoMapper;

namespace AutomapperProject
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var foo = new Foo { Id = 1, Name = "Foo" };
            var dto = MapperHelper.MapFrom<FooDto>(foo);
        }
    }

    public static class AutomapperExtensions
    {
        private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
        {
            foreach (string propName in map.GetUnmappedPropertyNames())
            {
                var srcPropInfo = map.SourceType.GetProperty(propName);

                if (srcPropInfo != null)
                    expr.ForSourceMember(propName, opt => opt.DoNotValidate());

                var destPropInfo = map.DestinationType.GetProperty(propName);

                if (destPropInfo != null)
                    expr.ForMember(propName, opt => opt.Ignore());
            }
        }

        public static void IgnoreUnmapped(this IProfileExpression profile)
        {
            profile.ForAllMaps(IgnoreUnmappedProperties);
        }
    }

    public static class MapperHelper
    {
      private static IMapper Mapper()
      {
        var mapperConfig = new MapperConfiguration(configuration=>{configuration.IgnoreUnmapped();});    
        return mapperConfig.CreateMapper();
      }

      public static T MapFrom<T>(object entity)
      {
          return Mapper().Map<T>(entity);
      }
    }

    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class FooDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Unmapped { get; set; }
    }
}
3
  • When you place a breakpoint at the start of the IgnoreUnmappedProperties method, do you get inside this method during debugging and do you get inside the foreach() loop you have inside this method? Please add the full error message you get from AutoMapper to your question, IIRC it include a more detailed description of which type/property has mapping problems. Commented Dec 26, 2019 at 14:56
  • I have tested it with 6.1.0 as well and in no version is the method IgnoreUnmappedProperties() called, which obviously means that the opt.Ignore() statements aren't called as well. Based on the source code above I can expect that, since you have no mappings defined, which would trigger for ForAllMaps(). For debugging purposes I have added a profile.CreateMap<Foo, FooDto>(); before the ForAllMaps() call and then the helper method gets called since there is one mapping added to the automapper. Please elaborate on what you are trying to do and why you thing it should work. Commented Dec 26, 2019 at 15:24
  • I edited my question by taken full error. My aim is running that code without any error and take 'dto' variable succesfully. It's working perfectly at AutoMapper.Extensions.Microsoft.DependencyInjection 6.1.0 version and I'm taking dto variable. But when I changed version to 7.0.0, it giving error at var dto = MapperHelper.MapFrom<FooDto>(foo); row @Progman . Did you take my error at 7.0.0 version? Commented Dec 26, 2019 at 15:33

2 Answers 2

9

The AutoMapper.Extensions.Microsoft.DependencyInjection package in version 7.0.0 requires the AutoMapper package in (at least) version 9.0.0. The AutoMapper.Extensions.Microsoft.DependencyInjection package in version 6.1.0 requires the AutoMapper package in (at least) version 8.1.0.

The change of AutoMapper from 8.0 to 9.0 has an important change which is affected in your code, as written in the 9.0 Upgrade Guide:

AutoMapper no longer creates maps automatically (CreateMissingTypeMaps and conventions)

You will need to explicitly configure maps, manually or using reflection. Also consider attribute mapping.

So with the new AutoMapper 9.0.0 you must define the mapping explicitly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Progman. You're right. I understood the reason of not working on 7.0.0. Because, static API was deprecated. But, I don't understand why it not working at 6.1.1 version. Because it require 8.1 not 9.0. But, it's not important for me from this time. Because I know that, it will be deprecated. So, I will use explicitly configure maps.
1

If you use Automapper package 9.0.0 or superior, you will need to explicitly configure maps. As @Progman said.

I resolve this downgrading the AutoMapper to 8.0.0 and AutoMapper.Extensions.Microsoft.DependencyInjection to version 6.0.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.