2017-02-17 2 views
0

엔티티의 리스팅 목적 및 세부 정보보기를 위해 DTO에 엔티티 매핑을 작성해야합니다. 하지만 목록을 위해 DTO 때문에 목록 속성을 무시해야합니다. DTO 때문에로드하지 않으려 고 Entity 프레임 워크에서 Lazy 로딩을 활성화했습니다. 목록보기 용 데이터를 쿼리하는 동안 동일한 엔티티의 두 매핑을 만들거나 속성에 대해 무시를 추가하려면 어떻게해야합니까?오토 매퍼에서 동일한 엔티티에 대해 다른 매핑을 어떻게 만들 수 있습니까?

cfg.CreateMap<page, PageViewModel>().ForMember(t => t.PageRows, opts => opts.MapFrom(s => s.page_rows)). 
      ForMember(t => t.PageRules, opts => opts.MapFrom(s => s.page_rules.Select(x => x.rule_id))); 

cfg.CreateMap<page, PageViewModel>().ForMember(t => t.PageRows, opts => opts.Ignore()). 
      ForMember(t => t.PageRules, opts => opts.MapFrom(s => s.page_rules.Select(x => x.rule_id))); 
+0

당신은 당신의 엔티티에 대한 두 개의 서로 다른 DTO 유형을 만들어야합니다. – Shad

+0

다른 솔루션이 없으므로 : (? –

+0

왜 별도의 DTO를 만들지 않으려합니까? – Amy

답변

1

당신은 매핑하는 두 가지 DTO 클래스를 생성해야합니다. 그렇게하고 싶지 않다면 두 개의 마커 인터페이스를 만들어 맵핑하는 또 다른 옵션이 있습니다.

2

당신은 설정 Func<ResolutionContext, bool>Precondition 다음은 Items 사전을 통해 제어 매개 변수를 전달하는 Action<IMappingOperationOptions>으로 Map 방법 오버로드를 사용할 수 있습니다. 예를 들어

: 다음

.ForMember(t => t.PageRows, opts => 
{ 
    opts.MapFrom(s => s.page_rows); 
    opts.PreCondition(context => !context.Items.ContainsKey("IgnorePageRows")); 
}) 

과 :

IEnumerable<page> source = ...; 
var withPageRows = Mapper.Map<List<PageViewModel>>(source); 
var noPageRows = Mapper.Map<List<PageViewModel>>(source, 
    opts => opts.Items.Add("IgnorePageRows", null)); 

참조 : Conditional mapping

관련 문제