我正在使用 Automapper 将一个对象属性复制到另一个,稍后将使用 EF 在数据库中更新。
问题是如何告诉 Automapper 复制每个属性但忽略特定属性(在本例中它将是 Id)。我是 AutoMapper 的新手,刚刚完成了这段代码。我在项目中没有其他配置或使用 AutoMap。
Mapper.Map(lead, existingLead);
我在这里下载了 AutoMapper 表格 https://github.com/AutoMapper/AutoMapper
最佳答案
在您的 Mapper.CreateMap<Type1, Type2>()
上你可以使用任何一个
.ForSourceMember(x => x.Id, opt => opt.Ignore())
或
.ForMember(x => x.Id, opt => opt.Ignore())
更新:
好像.Ignore()
重命名为 .DoNotValidate()
根据 AutoMapper docs .
关于c# - 忽略 AutoMapper 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898442/