AutoMapper.Mapper.Map 偶尔 returns 具有空属性的对象

AutoMapper.Mapper.Map sporadically returns object with null properties

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

                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;
                }

这是映射 interface/implementation class:

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));
    }
}

关于为什么会发生这种情况的任何想法?如何排除故障的提示?提前致谢!

那些 CreateMaps 应该只在启动时为每个 AppDomain 调用一次。确保在 App_Start 调用这些。发生的情况是您有竞争线程同时调用 CreateMap 和 Map。