我已经使用Microsoft Unity Framework配置Automapper,但是当我尝试像下面提到的代码那样进行映射时,我得到“缺少类型映射配置或不支持的映射。”
var orderResponse = this.Mapper.Map<Order, OrderV1>(order);
我无法弄清楚我在做错误的地方。
我已按照下面提到的步骤
个人资料制作
public class OrderProfile : Profile
{
public OrderProfile()
{
Mapper.CreateMap<Order, OrderV1>()
.ReverseMap();
}
}
2. Unity容器的注册
protected void Application_Start()
{
var container = InitializeUnity();
//Automapper Profile Registration
container.RegisterAutoMapperType(new ContainerControlledLifetimeManager());
}
public static IUnityContainer InitializeUnity()
{
using (IUnityContainer container = new UnityContainer())
{
------------
------------
return container ;
}
}
3.配置文件注册的扩展方法
public static class UnityExtensions
{
public static void RegisterAutoMapperType(this IUnityContainer container, LifetimeManager lifetimeManager = null)
{
RegisterAutoMapperProfiles(container);
var profiles = container.ResolveAll<Profile>();
var autoMapperConfigurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
profiles.Each(autoMapperConfigurationStore.AddProfile);
autoMapperConfigurationStore.AssertConfigurationIsValid();
container.RegisterInstance<IConfigurationProvider>(autoMapperConfigurationStore, new ContainerControlledLifetimeManager());
container.RegisterInstance<IConfiguration>(autoMapperConfigurationStore, new ContainerControlledLifetimeManager());
container.RegisterType<ConfigurationStore, ConfigurationStore>(new ContainerControlledLifetimeManager(), new InjectionConstructor(typeof(ITypeMapFactory), MapperRegistry.Mappers));
container.RegisterType<IConfigurationProvider, ConfigurationStore>( new ContainerControlledLifetimeManager());
container.RegisterType<IConfiguration, ConfigurationStore>();
container.RegisterType<ITypeMapFactory, TypeMapFactory>();
container.RegisterType<IMappingEngine, MappingEngine>(lifetimeManager ?? new TransientLifetimeManager(), new InjectionConstructor(typeof(IConfigurationProvider)));
}
private static void RegisterAutoMapperProfiles(IUnityContainer container)
{
IEnumerable<Type> autoMapperProfileTypes = AllClasses.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies())
.Where(type => type != typeof(Profile) && typeof(Profile).IsAssignableFrom(type));
autoMapperProfileTypes.Each(autoMapperProfileType =>
container.RegisterType(typeof(Profile),
autoMapperProfileType,
autoMapperProfileType.FullName,
new ContainerControlledLifetimeManager(),
new InjectionMember[0]));
}
}
答案 0 :(得分:0)
在您的个人资料中,调用CreateMap,而不是Mapper.CreateMap,并覆盖配置
public class OrderProfile : Profile {
protected override void Configure() {
base.CreateMap<Order, OrderV1>()
.ReverseMap();
}
}