2017-03-21 12 views
0

현재 내가지도하는 데 필요한 다음과 같은 개체가자동 매핑하는 것은 표에 가입하세요 제거하려면

OBJECT 나는 다음과 같은 객체

OBJECT 2

에 매핑하는 것을 시도하고있다 1

public class ContactInfo 
{ 
    public int ContactInfoId { get; set; } 
    public ICollection<PhoneToCustomer> Phones { get; set; } 
} 

public class PhoneToCustomer 
{ 
    public int ContactInformationId { get; set; } 
    public Phone Phone { get; set; } 
    public int PhoneId { get; set; } 
} 

public class Phone 
{ 
    public int PhoneId { get; set; } 
    public long Number { get; set; } 
    public PhoneType Type { get; set; } 
    public string Extension { get; set; } 
    public string CountryCode { get; set; } 
    public PhoneRestrictions Restrictions { get; set; } 
    public bool Primary { get; set; } 
} 

public class ContactInfoModel 
{ 
    public int ContactInfoId { get; set; } 
    public ICollection<PhoneModel> Phones { get; set; } 
} 

public class PhoneModel 
{ 
    public int PhoneId { get; set; } 
    public long Number { get; set; } 
    public PhoneType Type { get; set; } 
    public string Extension { get; set; } 
    public string CountryCode { get; set; } 
    public PhoneRestrictions Restrictions { get; set; } 
    public bool Primary { get; set; } 
} 

기본적으로 PhoneToCustomer 객체를 매핑 할 때 제거하려고합니다. 당신이 반대를 매핑 할 경우

AutoMapper.Mapper.CreateMap<Phone, PhoneModel>(); 

AutoMapper.Mapper.CreateMap<ContactInfo, ContactInfoModel>() 
       .ForMember(x => x.Phones, y => y.MapFrom(z => z.Phones.Select(q => q.Phone))); 

: 나는 다음과 같은 매핑을 추가 할 필요가 ContactInfoModel하는 CONTACTINFO을지도하기 위해 ContactInfo.PhoneToCustomer.Phone에

답변

1

(목록) 매핑 할 ContactInfoModel.Phones 필요

var contactInfo = new ContactInfo() 
    { 
     ContactInfoId = 1, 
     Phones = new List<PhoneToCustomer>() 
     { 
      new PhoneToCustomer() 
      { 
       Phone = new Phone(){CountryCode = "Code1", Extension = "Extension1"}, 
      }, 
      new PhoneToCustomer() 
      { 
       Phone = new Phone(){CountryCode = "Code2", Extension = "Extension2"} 
      } 
     } 
    }; 

var contactInfoModel = AutoMapper.Mapper.Map<ContactInfoModel>(contactInfo); 

var contactInfoBack = AutoMapper.Mapper.Map<ContactInfo>(contactInfoModel); 
: 여기
AutoMapper.Mapper.CreateMap<PhoneModel, Phone>(); 

    AutoMapper.Mapper.CreateMap<PhoneModel, PhoneToCustomer>() 
     .ForMember(x => x.Phone, y => y.MapFrom(z => z)) 
     .ForMember(x => x.ContactInformationId, y => y.Ignore()) 
     .ForMember(x => x.PhoneId, y => y.Ignore()); 

    AutoMapper.Mapper.CreateMap<ContactInfoModel, ContactInfo>(); 

사용량의 테스트 예입니다 ContactInfoModel에서 반대는 다음 매핑을 사용할 수 있습니다 CONTACTINFO하기