1
我在我的ASP.NET MVC应用程序中使用AutoMapper 6.0在实体和视图模型之间进行映射。实体正在使用字节[8]版本属性,但视图模型正在使用ulong版本属性。由于不同的属性类型,默认映射将忽略该字段,并以视图模型的默认值(即0)结束。所有映射的自定义AutoMapper名称/类型约定?
目前,我在每个_mapper.Map(entity,viewModel)之后调用下面的代码:
_mapper.Map(entity,viewModel);
viewModel.Version = System.BitConverter.ToUInt64(entity.Version.ToArray(), 0);
如何在初始化期间配置AutoMapper,以便不需要第二行?
我有上百个车型所以不是用ForMember(CFG)配置中创建自定义地图,我想修改AutoMapper约定,以便上述转换的类型发生在默认情况下为每个地图,如:
public class MyCustomProfile : AutoMapper.Profile
{
public MyCustomProfile()
{
CreateMissingTypeMaps = true;
//use custom converter for this convetion: ulong vVersion = BitConverter.ToUInt64(eVersion.ToArray(), 0);
}
}
几乎完美!谢谢!你已经解决了这个谜题。 你能否用上面例子的正确代码来编辑你的答案? CreateMap()。ConvertUsing(x => BitConverter.ToUInt64(x,0));谢谢! –
komsky