Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射-忽略带条件的映射_C#_Asp.net Mvc 3_Automapper - Fatal编程技术网

C# 自动映射-忽略带条件的映射

C# 自动映射-忽略带条件的映射,c#,asp.net-mvc-3,automapper,C#,Asp.net Mvc 3,Automapper,我正在使用automapper,我想知道当字段的映射为空时是否可以忽略它 这是我的密码: .ForMember(dest => dest.BusinessGroup_Id, opt => opt.MapFrom(src => (int)src.BusinessGroup)) src.BusinessGroup type=“enum” dest.BusinessGroup\u Id=int 它的目标是在src.BusinessGroup=null时启

我正在使用automapper,我想知道当字段的映射为空时是否可以忽略它

这是我的密码:

.ForMember(dest => dest.BusinessGroup_Id, 
           opt => opt.MapFrom(src => (int)src.BusinessGroup))
  • src.BusinessGroup type=“enum”
  • dest.BusinessGroup\u Id=int
它的目标是在src.BusinessGroup=null时启用该映射。

我认为option会起作用

.ForMember(d => d.BusinessGroup_Id, o => o.MapFrom(s => (int?)s.BusinessGroup));
.ForMember(d => d.BusinessGroup_Id, o => o.NullSubstitute(0));
顺便说一句,您可以在映射操作中写入您的条件:

.ForMember(d => d.BusinessGroup_Id,
           o => o.MapFrom(s => s.BusinessGroup == null ? 0 : (int)s.BusinessGroup));   
更新如果无法将某些默认值指定给属性,则可以忽略该属性并仅映射而不映射为null:

.ForMember(d => d.BusinessGroup_Id, o => o.Ignore())
.AfterMap((s, d) =>
    {
        if (s.BusinessGroup != null)
            d.BusinessGroup_Id = (int)s.BusinessGroup;
    });

嗨,拉兹别列佐夫斯基,谢谢你的快速回复!我无法将0设置为BusinessGroup\u Id,因为这是DB上的foreingKey