我有两个类如下:
public class Rule
{
public int Id { get; set; }
public RuleGroup RuleGroup { get; set; }
}
public class RuleGroup
{
public int Id { get; set; }
public List<Rule> RuleList { get; set; }
}
RuleGroup有一个规则列表。我的AutoMapper设置如下:
Mapper.CreateMap<RuleRecord, FirstSolar.Mes.Core.Entities.Recipe.Rule>()
.ForMember(destination => destination.RuleGroup, source => source.Ignore())
.ForMember(destination => destination.Id, source => source.MapFrom(item => item.RuleId));
Mapper.CreateMap<IList<RuleRecord>, IList<FirstSolar.Mes.Core.Entities.Recipe.Rule>>();
Mapper.CreateMap<RuleGroupRecord, FirstSolar.Mes.Core.Entities.Recipe.RuleGroup>()
.ForMember(destination => destination.Id, source => source.MapFrom(item => item.RuleGroupId));
Mapper.CreateMap<IList<RuleGroupRecord>, IList<FirstSolar.Mes.Core.Entities.Recipe.RuleGroup>>();
当我尝试将RuleGroupRecord(LinqToSQL对象)映射到RuleGroup(DTO)时,AutoMapper说我需要为RuleGroup.RuleList添加映射。我想知道为什么,因为我定义了如何映射单个RuleRecord和List。
如果必须,我该怎么做?
答案 0 :(得分:2)
简单地添加(我希望我的语法正确,但你应该看到我暗示的内容):
.ForMember(destination => destination.RuleList, source => source.MapFrom(item => item.Rules));
到第二个映射。当您在第一个映射中处理RuleRecord
到Rule
的常规映射时,您没有告诉automapper映射特定属性RuleGroup.RuleList
。