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; }
}
}
IgnoreUnmappedPropertiesmethod, do you get inside this method during debugging and do you get inside theforeach()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.IgnoreUnmappedProperties()called, which obviously means that theopt.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 forForAllMaps(). For debugging purposes I have added aprofile.CreateMap<Foo, FooDto>();before theForAllMaps()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.AutoMapper.Extensions.Microsoft.DependencyInjection 6.1.0version and I'm takingdtovariable. But when I changed version to 7.0.0, it giving error atvar dto = MapperHelper.MapFrom<FooDto>(foo);row @Progman . Did you take my error at 7.0.0 version?