【问题标题】:AutoMapper.Mapper.Map sporadically returns object with null propertiesAutoMapper.Mapper.Map 偶尔返回具有空属性的对象
【发布时间】:2015-08-11 17:12:02
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我也怀疑。似乎是一种竞争条件。
    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多