且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果并非所有源属性都匹配,则AutoMapper无法阻止空源值

更新时间:2023-02-22 08:02:22

此解决了!

https://github.com/AutoMapper/AutoMapper/issues/432

引用文章

我们刚刚从3.0.0升级到3.1.0,并开始遇到具有以下定义的映射问题:

We just upgraded from 3.0.0 to 3.1.0 and started getting an issue with mappings with the following definition:

.ForAllMembers(o => o.Condition(c =>!c.IsSourceValueNull));这是 以前可以正常运行,因此不会尝试映射 没有源值的属性.升级后, 似乎Automapper面对的目标成员具有 没有匹配的源成员,将尝试从源类型映射到 目标成员.然后,这将引发映射异常,因为 没有针对任何类型的源类型的映射定义 目标属性类型.以前,Automapper似乎 正确地忽略没有匹配的源成员的成员.

.ForAllMembers(o => o.Condition(c => !c.IsSourceValueNull)); This was previously functioning correctly and would not attempt to map properties which did not have a source value. After the upgrade, it seems that Automapper, when faced with a destination member that has no matching source member, will attempt to map from the source type to the destination member. This then throws a mapping exception because there is no mapping definition for the source type to any of the destination property types. Previously, Automapper seemed to rightfully ignore members which did not have a matching source member.

We changed the condition line to:

.ForAllMembers(o => o.Condition(c => c.PropertyMap.SourceMember != null && !c.IsSourceValueNull));