使用C#automapper映射引用的表

时间:2017-09-28 05:20:25

标签: c# entity-framework automapper dto

假设我有一个表Scopes,其中包含另一个表(subscopes)的外键,我想映射它。我的Scopes表中可用的所有列以及我引用的表中的一些列(subscopes)都需要映射到DTO。

我的问题是:

  1. DTO的内容应该是什么?
  2. 我应该如何使用c#和Automapper进行映射?

1 个答案:

答案 0 :(得分:0)

this.CreateMap<tblSubScope, Sub2MainScopeDto>()
          .ForMember(t => t.IdxSubScope, opt => opt.MapFrom(s => s.idxSubScope))
          .ForMember(t => t.IdxMainScope, opt => opt.MapFrom(s => s.idxMainScope))
          .ForMember(t => t.SubScopeDescription, opt => opt.MapFrom(s => s.strSubScope))
          .ForMember(t => t.MainScopeDescription, opt => opt.MapFrom(s => s.tblMainScope.strMainScope))
          .ReverseMap()
          .ForMember(t => t.idxSubScope, opt => opt.MapFrom(s => s.IdxSubScope))
          .ForMember(t => t.strSubScope, opt => opt.MapFrom(s => s.SubScopeDescription))
          .ForMember(t => t.idxMainScope, opt => opt.MapFrom(s => s.IdxMainScope));

解决了问题。