Updating to Automapper v5 has error with .NullSubstitute

I'm updating our automapper libraries from version 3 to version 5.2.0 and I'm getting this error

System.InvalidOperationException: System.InvalidOperationException: No coercion operator is defined between types 'System.String' and 'System.Int32'

on this line of code in the mapping setup for two complex objects.

cfg.CreateMap<objA, objB>()
.ForAllMembers(config => config.NullSubstitute(string.Empty));

I'm guessing this is happening because it can't set int to null, and it's being explicit about it now, and in v3 of automapper it just ignored it. Can someone confirm that? Do I have to explicitly map each member type in version 5?

For this mapping the object has ints, bools and strings. Is there a way to tell it to map the strings to String.Empty when they are null and ignore the other types?


Unfortinately this is the AM5 behavior for NullSubstitute and also ForAllMembers does not support filtering and does not provide any information about the member being mapped.

As a workaround I would suggest performing a manual "fix up" using the ForAllPropertyMaps method which allows filtering and provides the necessary information:

cfg.ForAllPropertyMaps(
    pm => "".Equals(pm.NullSubstitute) && pm.DestinationPropertyType != typeof(string),
    (pm, m) => pm.NullSubstitute = null
);
链接地址: http://www.djcxy.com/p/37420.html

上一篇: 将字符串值转换为Automapper中的实体

下一篇: 更新到Automapper v5与.NullSubstitute错误