AutoMapper.Mapper.Map偶尔返回具有null属性的对象

时间:2015-08-11 17:12:03

标签: c# asp.net-mvc entity-framework automapper

我在MVC / EF应用程序中使用Automapper。有时,当我启动我的应用程序进行调试(F5)时,AutoMapper.Mapper.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));
    }
}

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

1 个答案:

答案 0 :(得分:3)

在启动时,每个AppDomain只应调用一次CreateMaps。确保在App_Start中调用它们。发生的事情是你有同时调用CreateMap和Map的竞争线程。