2

我正在从 nuget 更新我的 automapper 4.2.1,它安装了 automapper 6.1.1 版本。在旧版本中,所有映射都可以正常工作,但是当我更新我的自动映射器库映射时,它说缺少类型映射配置或不支持的映射。使用时映射类型Mapper.Map<Class1,Class2>(result)。我正在使用配置文件进行配置global.asax。我的项目是 C# MVC,这是我的代码;

在 Global.asax

 public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
           AutoMapperConfigurations.Configure();
        }
    }

我的映射器配置

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 -> TreeCategoryDtos Project.Models.ViewModel.TreeCategoryViewModel -> Project.BusinessLayer.CategoriesBL.Dtos.TreeCategoryDtos)


现在错误消息已更改。这就是我现在得到的“映射器未初始化。使用适当的配置调用初始化。如果您尝试通过容器或其他方式使用映射器实例,请确保您没有对静态 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 中调用我的个人资料。你可以在上面看到这些代码

4

1 回答 1

1

您只能调用Mapper.Initialize 一次- 使用此代码,每次加载配置文件时都会覆盖配置。

您的个人资料应该继承自Profile- 请参阅文档

于 2017-06-30T11:34:54.543 回答