AutoMapper.映射器.映射偶尔返回具有null属性的对象

本文关键字:映射 null 属性 对象 偶尔 返回 AutoMapper | 更新日期: 2023-09-27 18:07:32

我在MVC/EF应用程序中使用Automapper。偶尔,当我启动我的应用程序进行调试(F5(时,AutoMapper。映射器。Map返回一个具有null属性值的映射对象。我需要重新启动才能自行解决。将应用程序部署到远程服务器时也会出现此问题。作为一种变通方法,我必须检查null属性,然后手动映射我的对象。

                ProductHierarchyRow mappedObj = _mapProvider.Map<VW_PROD_HIERARCHY,ProductHierarchyRow>(foundRow);
                if (String.IsNullOrEmpty(mappedObj.DepartmentId)) //Test one of the properties
                {
                    //Manually map because Automapper has failed for some reason
                    mappedObj = new ProductHierarchyRow();
                    mappedObj.MarketChannel = foundRow.MARKETCHANNEL;
                    mappedObj.BrandId = foundRow.BRAND_ID;
                    mappedObj.BrandLabel = foundRow.BRAND_LABEL;
                    mappedObj.DivisionId = foundRow.DIVISION_ID;
                    mappedObj.DivisionLabel = foundRow.DIVISION_LABEL;
                    mappedObj.DepartmentId = foundRow.DEPARTMENT_ID;
                    mappedObj.DepartmentLabel = foundRow.DEPARTMENT_LABEL;
                    mappedObj.ClassId = foundRow.CLASS_ID;
                    mappedObj.ClassLabel = foundRow.CLASS_LABEL;
                    mappedObj.SubclassId = foundRow.SUBCLASS_ID;
                    mappedObj.SubclassLabel = foundRow.SUBCLASS_LABEL;
                }

这是映射接口/实现类:

public class MappingProvider : IMappingProvider
{
    public MappingProvider()
    {
        AutoMapper.Mapper.CreateMap<VW_PROD_HIERARCHY, ProductHierarchyRow>().ForAllMembers(opt => opt.Ignore());
        AutoMapper.Mapper.CreateMap<VW_PROD_HIERARCHY, ProductHierarchyRow>()
                .ForMember(x => x.MarketChannel, o => o.ResolveUsing(s => s.MARKETCHANNEL))
                .ForMember(x => x.BrandId, o => o.ResolveUsing(s => s.BRAND_ID))
                .ForMember(x => x.BrandLabel, o => o.ResolveUsing(s => s.BRAND_LABEL))
                .ForMember(x => x.DivisionId, o => o.ResolveUsing(s => s.DIVISION_ID))
                .ForMember(x => x.DivisionLabel, o => o.ResolveUsing(s => s.DIVISION_LABEL))
                .ForMember(x => x.DepartmentId, o => o.ResolveUsing(s => s.DEPARTMENT_ID))
                .ForMember(x => x.DepartmentLabel, o => o.ResolveUsing(s => s.DEPARTMENT_LABEL))
                .ForMember(x => x.ClassId, o => o.ResolveUsing(s => s.CLASS_ID))
                .ForMember(x => x.ClassLabel, o => o.ResolveUsing(s => s.CLASS_LABEL))
                .ForMember(x => x.SubclassId, o => o.ResolveUsing(s => s.SUBCLASS_ID))
                .ForMember(x => x.SubclassLabel, o => o.ResolveUsing(s => s.SUBCLASS_LABEL));
    }
    public T1 Map<T, T1>(T entry)
    {
        return AutoMapper.Mapper.Map<T, T1>(entry);
    }
    public T1 Map<T, T1>(T source, T1 dest)
    {
        return (T1)AutoMapper.Mapper.Map(source, dest, typeof(T), typeof(T1));
    }
}

你知道为什么会这样吗?如何排除故障的提示?提前感谢!

AutoMapper.映射器.映射偶尔返回具有null属性的对象

这些CreateMaps在启动时每个AppDomain只能调用一次。确保在应用程序启动时调用这些程序。现在的情况是,您有同时调用CreateMap和Map的竞争线程。