使用AutoMapper更新数据库复杂对象:如何单次映射时忽略嵌套对象?
Absolutely! You have a few solid options to tell AutoMapper to ignore those nested ClassThree and ClassFour properties so they don't get overwritten to null. Let's walk through the most common approaches:
1. Update Your AutoMapper Profile (Recommended for Reuse)
This is the cleanest long-term solution—define your mapping rules once in a profile, so they apply every time you use Map().
Create a profile class that explicitly ignores the nested properties you don't want to update:
public class ClassOneMappingProfile : Profile { public ClassOneMappingProfile() { // Map ClassOne to itself, including the ClassTwo reference CreateMap<ClassOne, ClassOne>() .ForMember(dest => dest.ClassTwoExample, opt => opt.MapFrom(src => src.ClassTwoExample)); // Map ClassTwo to itself, ignoring the nested reference properties CreateMap<ClassTwo, ClassTwo>() .ForMember(dest => dest.ClassThreeExample, opt => opt.Ignore()) .ForMember(dest => dest.ClassFourExample, opt => opt.Ignore()); } }
Register this profile with AutoMapper (usually in your startup/config code), and now every call to _mapper.Map(model, oldClassOne) will automatically skip over ClassThreeExample and ClassFourExample, leaving their existing values intact.
2. Inline Ignore During Map Call (One-Off Scenario)
If you only need to ignore these properties for this specific mapping operation (and not globally), you can use inline configuration:
function (ClassOne model) { var oldClassOne = _repos.GetClassOne(model.ID); _mapper.Map(model, oldClassOne, opts => { // Ignore the nested reference properties directly opts.ForMember(dest => dest.ClassTwoExample.ClassThreeExample, opt => opt.Ignore()); opts.ForMember(dest => dest.ClassTwoExample.ClassFourExample, opt => opt.Ignore()); }); await _repos.SaveAllAsync(); }
This is handy for one-off cases, but if you need this behavior multiple times, the profile approach is better to avoid code duplication.
3. Use a DTO to Restrict Mapped Properties
Another clean approach is to create a Data Transfer Object (DTO) that only includes the properties you want to update. AutoMapper will only map the properties present in the DTO, leaving others untouched.
Example DTOs:
public class ClassOneUpdateDto { public int ID { get; set; } public string Name { get; set; } public ClassTwoUpdateDto ClassTwoExample { get; set; } } public class ClassTwoUpdateDto { public int ID { get; set; } public string Name { get; set; } // No ClassThree or ClassFour properties included here }
Then update your mapping profile to map from the DTO to your entity:
CreateMap<ClassOneUpdateDto, ClassOne>(); CreateMap<ClassTwoUpdateDto, ClassTwo>();
Now when you map, use the DTO instead of the full ClassOne model:
function (ClassOneUpdateDto model) { var oldClassOne = _repos.GetClassOne(model.ID); _mapper.Map(model, oldClassOne); await _repos.SaveAllAsync(); }
This approach makes your intent clear and prevents accidental overwrites of properties you didn't mean to touch.
内容的提问来源于stack exchange,提问作者Daath

