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; }
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With