简体   繁体   English

如何使用AutoMapper将空值映射到特定映射?

[英]How to map null values with AutoMapper for an specific mapping?

I found this solution AutoMapper - setting destination string to null actually makes it string.Empty 我发现这个解决方案AutoMapper - 将目标字符串设置为null实际上使它成为string.Empty

but It uses the Initialize method so all the mappings behavior would change along the app : O 但它使用Initialize方法,因此所有映射行为都会沿着app改变:O

And I just need do it for a specific mapping. 我只需要为特定的映射做这件事。

Found a solution: 找到了解决方案:

Mapper.Initialize( Conf =>
  {
    Conf.ForSourceType<MyGreatViewModel>().AllowNullDestinationValues = true;
  } );

Thanks guys : ) 多谢你们 : )

Take care of your destination field types (remember you need nullable types. eg 'Byte?' ) 照顾你的目的地字段类型(记住你需要可以为空的类型。例如'Byte?')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM