【问题标题】:Mapping referenced table with C# automapper使用 C# automapper 映射引用的表
【发布时间】:2017-09-28 05:20:24
【问题描述】:

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

我的问题是:

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

【问题讨论】:

  • 1. DTO 的内容取决于您的要求和偏好。 2. 取决于你构建的 DTO
  • 有什么代码可以给我们看吗?我不知道您实际面临的是哪个问题。

标签: c# entity-framework automapper dto


【解决方案1】:
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));

问题解决了。

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 2011-08-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多