2012-08-25 2 views
0

이 명령문은 사람, 주소 및 전화 번호 목록을 반환합니다.eager-loading 및 collection과 함께 AutoMapper를 어떻게 사용합니까?

var listOfPeople = db.People.AsQueryable(); 

이제 AutoMapper를 사용하여 위의 LINQ 문의 결과를 뷰 모델에 매핑하고 싶습니다. 뷰 모델은 주로 일부 속성이 클라이언트/사용자에게 반환되지 않도록하기 위해 만들어졌습니다.

어떻게 AutoMapper가 기본 오브젝트, Person 구성된 뷰 모델로, 결과, listOfPeople지도에 도착하고, AddressesPhoneNunmbersICollections합니까? 한 사람을 단일 VM에 매핑하는 데 문제가 없습니다. 컬렉션 컬렉션을 포함하고있는 listOfPeople 컬렉션 매핑에 매달린 것 같습니다. 내가 여기

MVC 4 ASP.NET Web API 4,하지를 사용하고

는, 뷰 모델 나는 당신의 질문을 이해하면

public class BasicApiViewModel 
{ 
    public int Id { get; set; } 

    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public virtual ICollection<Address> Addresses { get; set; } 

    public virtual ICollection<Phone> Phones { get; set; } 

} 

답변

0

별도로 수집 매핑을 구성 할 필요가 없습니다 당신은 필요 이미 완료 한 것처럼 클래스 간의 매핑을 정의하면 컬렉션이 자동으로 처리됩니다.

AutoMapper는이 아닌 요소 유형의 구성 만 필요합니다.사용할 수있는 배열 또는 목록 유형입니다. 모든 기본 일반적인 수집 유형이 지원됩니다

public class Source 
{ 
    public int Value { get; set; } 
} 

public class Destination 
{ 
    public int Value { get; set; } 
} 

: 나는 또한 필요

Mapper.CreateMap<Source, Destination>(); 

var sources = new[] 
    { 
     new Source { Value = 5 }, 
     new Source { Value = 6 }, 
     new Source { Value = 7 } 
    }; 

IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources); 
ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources); 
IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);  
List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources); 
Destination[] arrayDest = Mapper.Map<Source[], Destination[]>(sources); 

Lists and arrays

+0

예를 들어, 우리는 간단한 소스 및 대상 유형이있을 수 있습니다 전화 번호 및 주소록 컬렉션의 클래스를 매핑합니다. 그런 다음 모든 것이 예상대로 작동했습니다. 감사! – Scott

관련 문제