我从nuget更新了我的automapper 4.2.1,它是安装automapper 6.1.1版本。在旧版本中,所有映射都能正常工作,但是当我更新我的automapper库时,映射不起作用,它表示缺少类型映射配置或不支持的映射。使用Mapper.Map<Class1,Class2>(result)
时映射类型。我在global.asax
中使用配置文件进行配置。我的项目是C#MVC,这是我的代码;
在Global.asax
中 public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AutoMapperConfigurations.Configure();
}
}
我的Mapper配置
public class AutoMapperConfigurations
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<ModeltoDTOMappingsProfile>();
cfg.AddProfile<DTOtoDTOModelMappingsProfile>();
cfg.AddProfile<DTOtoViewModelMappingsProfile>();
//cfg.ForAllMaps((tm, me) => me.ForAllMembers(option => option.Condition((source, destination, sourceMember) => sourceMember != null)));
});
Mapper.AssertConfigurationIsValid();
}
这是我的一个个人资料示例代码
public class DTOtoViewModelMappingsProfile: Profile
{
public DTOtoViewModelMappingsProfile()
{
Mapper.Initialize(cfg =>
{
// Also I tried these one but It didn't work
//cfg.CreateMissingTypeMaps = true;
//cfg.AllowNullCollections = true;
//cfg.AllowNullDestinationValues = false;
//cfg.ForAllMaps((tm, me) => me.ForAllMembers(option => option.Condition((source, destination, sourceMember) => sourceMember != null)));
#region Category
cfg.CreateMap<TreeCategoryViewModel, TreeCategoryDtos>()
.ForMember(dest => dest.T_C_ID, opt => opt.MapFrom(src => src.CId))
.ForMember(dest => dest.T_C_DESC, opt => opt.MapFrom(src => src.CDesc))
.ForMember(dest => dest.T_C_INUSE, opt => opt.MapFrom(src => src.CInuse))
.ForMember(dest => dest.T_IS_TECH, opt => opt.MapFrom(src => src.IsTech))
.ForMember(dest => dest.T_IS_BRANCH, opt => opt.MapFrom(src => src.IsBranch))
.ForMember(dest => dest.T_IS_MARKETING, opt => opt.MapFrom(src => src.IsMarketing))
.ForMember(dest => dest.T_IS_SUPPORT, opt => opt.MapFrom(src => src.IsSupport))
.ForMember(dest => dest.T_C_LEVEL, opt => opt.MapFrom(src => src.CLevel))
.ForMember(dest => dest.T_C_ORDER, opt => opt.MapFrom(src => src.COrder))
.ForMember(dest => dest.T_C_PARENT, opt => opt.MapFrom(src => src.CParent))
.ForMember(dest => dest.TREE_CATEGORY1, opt => opt.ResolveUsing(src => src.TreeCategoryDtos1))
.ForMember(dest => dest.TREE_CATEGORY2, opt => opt.ResolveUsing(src => src.TreeCategoryDtos2))
;
cfg.CreateMap<TreeCategoryDtos, TreeCategoryViewModel>()
.ForMember(dest => dest.CId, opt => opt.MapFrom(src => src.T_C_ID))
.ForMember(dest => dest.CDesc, opt => opt.MapFrom(src => src.T_C_DESC))
.ForMember(dest => dest.CInuse, opt => opt.MapFrom(src => src.T_C_INUSE))
.ForMember(dest => dest.IsTech, opt => opt.MapFrom(src => src.T_IS_TECH))
.ForMember(dest => dest.IsBranch, opt => opt.MapFrom(src => src.T_IS_BRANCH))
.ForMember(dest => dest.IsMarketing, opt => opt.MapFrom(src => src.T_IS_MARKETING))
.ForMember(dest => dest.IsSupport, opt => opt.MapFrom(src => src.T_IS_SUPPORT))
.ForMember(dest => dest.CLevel, opt => opt.MapFrom(src => src.T_C_LEVEL))
.ForMember(dest => dest.COrder, opt => opt.MapFrom(src => src.T_C_ORDER))
.ForMember(dest => dest.CParent, opt => opt.MapFrom(src => src.T_C_PARENT))
.ForMember(dest => dest.TreeCategoryDtos1, opt => opt.ResolveUsing(src => src.TREE_CATEGORY1))
.ForMember(dest => dest.TreeCategoryDtos2, opt => opt.ResolveUsing(src => src.TREE_CATEGORY2))
;
#endregion
});
Mapper.AssertConfigurationIsValid();
}
在控制器
中var result = Mapper.Map<TreeCategoryViewModel,TreeCategoryDtos>(treeCategories);
这一行是例外。为什么我收到此错误?。(缺少类型映射配置或不支持的映射。映射类型:TreeCategoryViewModel - &gt; TreeCategoryDtos Project.Models.ViewModel.TreeCategoryViewModel - &gt; Project.BusinessLayer.CategoriesBL.Dtos.TreeCategoryDtos)
现在错误消息已更改。这就是我现在得到的“ Mapper未初始化。使用适当的配置调用Initialize。如果您尝试通过容器或其他方式使用映射器实例,请确保您没有对静态Mapper.Map的任何调用。方法,如果您正在使用ProjectTo或UseAsDataSource扩展方法,请确保传入适当的IConfigurationProvider实例。“
但错误来自这个类。在我的模型映射中。
public class ModeltoDTOMappingsProfile : Profile
{
public ModeltoDTOMappingsProfile()
{
CreateMap<TREE_CATEGORY, TreeCategoryDtos>();
CreateMap<TreeCategoryDtos, TREE_CATEGORY>()
.ForMember(dest => dest.G_MARK_FOR_DELETE, opt => opt.Ignore())
.ForMember(dest => dest.L_INSERTDATE, opt => opt.Ignore())
.ForMember(dest => dest.L_INSERTUSER, opt => opt.Ignore())
.ForMember(dest => dest.L_UPDATEDATE, opt => opt.Ignore())
.ForMember(dest => dest.L_UPDATEUSER, opt => opt.Ignore());
}
}
我正在使用AutomapperConfigurations在global.asax中调用我的个人资料。您可以在上面看到这些代码
答案 0 :(得分:1)
您必须只使用此代码拨打Mapper.Initialize
一次,每次加载配置文件时都要覆盖配置。
您的个人资料应继承自Profile
- 请参阅the documentation