AutoMapper-将目标字符串设置为null实际上使它成为string.Empty [英] AutoMapper - setting destination string to null actually makes it string.Empty

查看:175
本文介绍了AutoMapper-将目标字符串设置为null实际上使它成为string.Empty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下映射:

Mapper.CreateMap<ObjectA, ObjectB>()
    .ForMember(dest => dest.SomeStringProperty, opt => opt.MapFrom(src => null))

SomeStringProperty现在是空字符串,不为空(正如我期望的那样)

SomeStringProperty is now empty string not null (as I would expect)

这是一个错误吗?我怎样才能使它实际上为空?

Is this a bug? How can I get it to actually be null?

我看到opt.Ignore()会将其设置为null,但实际上我想像下面这样执行条件空值,而上述简化的bug(?)可以防止这种情况

I see that opt.Ignore() will make it null but I actually want to do a conditional null like the following and the above simplified bug(?) is preventing this

Mapper.CreateMap<ObjectA, ObjectB>()
    .ForMember(dest => dest.SomeStringProperty, opt => opt.MapFrom(src => src.SomeOtherProp != null ? src.SomeOtherProp.Prop1 : null))

推荐答案

我在查看源代码后发现了该设置...确认这不是错误,而是实际上是可配置的设置.

I found the setting after looking through the source code... Confirming that this is not a bug, but in fact a configurable setting.

当我配置映射时..

Mapper.Initialize(x =>
{
    x.AddProfile<UIProfile>();
    x.AddProfile<InfrastructureProfile>();
    x.AllowNullDestinationValues = true; // does exactly what it says (false by default)
});

这篇关于AutoMapper-将目标字符串设置为null实际上使它成为string.Empty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆