CreateMap<SaveMaterialDto, Material>().Ignore(x => x.Id).ForAllMembers(opt => opt.Condition((src, dest, sreMember, dMember) => sreMember != null));

This code snippet illustrates how to prevent mapping when a string property is 'null'. However, for value types like 'int', 'bool', and 'float', the mapping process handles null values automatically. When the source value is 'null', value types are assigned their default values. For instance, an 'int' property will be mapped to 0, and a 'bool' property will be mapped to 'false' when the source value is 'null'.

Consequently, you don't need to explicitly define conditions for value types in your mapping configuration. AutoMapper will automatically map them to their default values.

AutoMapper: Mapping Value Types with Null Values

原文地址: https://www.cveoy.top/t/topic/qlAk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录