[英]Can't use AutoMapper to map to null value
public class TestNullableString {
public string? Test;
public TestNullableString(string? test) {
Test = test;
}
}
public class TestNonNullableString {
public string Test;
}
public class TestProfile : Profile
{
public TestProfile() {
CreateMap<TestNonNullableString, TestNullableString>
.ForMember(dest => dest.Test, opt => {
opt.AllowNull();
opt.MapFrom(src => src.Test == "" ? null : src.Test);
});
}
}
If I try to map from NonNullableString to NullableString, it still gives me the empty string at mapping.如果我尝试从 NonNullableString 映射到 NullableString,它仍然在映射时给我空字符串。 It wont give me a null-value, even if NonNullableString is "".它不会给我一个空值,即使 NonNullableString 是“”。 What am I doing wrong?我究竟做错了什么?
I'm using AutoMapper 9.0.0.我正在使用 AutoMapper 9.0.0。
PS.附注。 I've also tried setting AllowNullDestinationValues to true without any success.我也试过将 AllowNullDestinationValues 设置为 true 没有任何成功。
CreateMap<TestNonNullableString, TestNullableString>()
.ForMember(dest => dest.Test,
opt => { opt.MapFrom(src => string.IsNullOrWhiteSpace(src.Test) ? null : src.Test); })
.ConstructUsing(x => new TestNullableString(string.IsNullOrWhiteSpace(x.Test) ? null: x.Test ));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.