2017-12-15 2 views
0

작업 신청 자격이 있습니다. 신청자가 모든 정보를 기입하고 신청서를 제출하면 저장하고 싶습니다. 현재 데이터 모델 (개인 정보, 가용성 등)과 관련하여 애플리케이션의 각 부분을 저장하는 구성 요소가 있습니다. 실행하면 다음과 같은 예외가 발생합니다.EF 엔티티에 DTO 매핑 예외

매핑되지 않은 멤버가 있습니다. 아래 유형 및 회원을 검토하십시오. 생성자의 모든 매개 변수 사용자 지정 해결을 추가, 무시, 커스텀 매핑 식을 추가하거나 수정 소스/대상 유형

을 일치하는 생성자를 들어, 인수 없음의 ctor 추가, 선택적 인수를 추가하거나지도 가 CandidateApplications CandidateAvailabilities을 AddressId :

매핑되지 않은 속성> EmploymentApplication.Entities.Candidate (대상 멤버리스트) -

CandidateDto - (대상 멤버리스트) EmploymentApplication.Common.DataTransferObjects.CandidateDto> 후보 CandidateEducations CandidateEmploymentHistories CandidateReferences CandidateTeleLicenses

나는 MapFrom 문에 나머지의 AddressId를 지정하는 것을 시도했다, 그들은 매핑 초기화에 내가 무시 말한 EF 탐색 속성입니다. 불행히도, 오류가 지속되고 나는 지금 무엇을 해야할지 모른다.

public class CandidateDto 
{ 
    public Guid CandidateId { get; set; } 
    public AddressDto Address { get; set; } 
    public UserAccountDto UserAccount { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public DateTime DateOfBirth { get; set; } 
    public string PrimaryPhone { get; set; } 
    public string Email { get; set; } 
    public bool HasWorkEligibility { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateModified { get; set; } 
    public Guid CreatedBy { get; set; } 
    public Guid ModifiedBy { get; set; } 
    public Guid UserAccountId { get; set; } 
} 

그리고 :

여기
 public void SaveCandidateInfo(CandidateDto candidateDto) 
    { 
     var candidateInfoToAdd = _mapper.Map<Candidate>(candidateDto); 
     _candidateRepository.Add(candidateInfoToAdd); 
     _candidateRepository.Save(); 
    } 

는 DTO입니다 : 여기

 Mapper.Initialize(m => m.CreateMap<Candidate, CandidateDto>()); 
     Mapper.Initialize(m => m.CreateMap<CandidateDto, Candidate>() 
      .ForMember(dest => dest.AddressId, opt => opt.MapFrom(src => src.Address.AddressId)) 
      .ForMember(dest => dest.CandidateApplications, opt => opt.Ignore()) 
      .ForMember(dest => dest.CandidateAvailabilities, opt => opt.Ignore()) 
      .ForMember(dest => dest.CandidateEducations, opt => opt.Ignore()) 
      .ForMember(dest => dest.CandidateEmploymentHistories, opt => opt.Ignore()) 
      .ForMember(dest => dest.CandidateReferences, opt => opt.Ignore()) 
      .ForMember(dest => dest.CandidateTeleLicenses, opt => opt.Ignore()) 
     ); 

가 매핑이 실제로 발생하는 구성 요소 방법 : 여기

내 매핑을 살펴입니다 마지막으로, 후보자의 EF 수업이 있습니다 :

public partial class Candidate 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public Candidate() 
    { 
     this.CandidateApplications = new HashSet<CandidateApplication>(); 
     this.CandidateAvailabilities = new HashSet<CandidateAvailability>(); 
     this.CandidateEducations = new HashSet<CandidateEducation>(); 
     this.CandidateEmploymentHistories = new HashSet<CandidateEmploymentHistory>(); 
     this.CandidateReferences = new HashSet<CandidateReference>(); 
     this.CandidateTeleLicenses = new HashSet<CandidateTeleLicense>(); 
    } 

    public System.Guid CandidateId { get; set; } 
    public Nullable<System.Guid> AddressId { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public System.DateTime DateOfBirth { get; set; } 
    public string PrimaryPhone { get; set; } 
    public string Email { get; set; } 
    public bool HasWorkEligibility { get; set; } 
    public System.DateTime DateCreated { get; set; } 
    public System.DateTime DateModified { get; set; } 
    public System.Guid CreatedBy { get; set; } 
    public System.Guid ModifiedBy { get; set; } 
    public System.Guid UserAccountId { get; set; } 

    public virtual Address Address { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateApplication> CandidateApplications { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateAvailability> CandidateAvailabilities { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateEducation> CandidateEducations { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateEmploymentHistory> CandidateEmploymentHistories { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateReference> CandidateReferences { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<CandidateTeleLicense> CandidateTeleLicenses { get; set; } 
    public virtual UserAccount UserAccount { get; set; } 
} 

도움 주셔서 감사합니다.

+0

당신 구성 거꾸로 보인다. 당신은'CreateMap ()'를 가지고 있지만'.ForMember (dest => dest.AddressId, opt => opt.MapFrom (src => src.Address.AddressId))'를 수행합니다. CandidateDto는 Address 속성을 가지고 있다고 말합니다. 그리고 Initialize를 하나만하면됩니다. – Jasen

+0

후보가 주소 속성을 갖고 있지 않습니까? – keyle56

+0

어떤 AM 버전을 사용하고 있습니까? 또한 전체 repro (a.k.a. [mcve]) –

답변

0

대상 속성에 대한 일부 구성을 정의하여 무시했지만 매핑하려고하면 Map 메서드를 전달해야하는 대상 인스턴스가 없습니다. 따라서 무시 된 속성에는 아무런 문제가 없습니다.

그래서 변경해야합니다.

var candidateInfoToAdd = _mapper.Map<Candidate>(candidateDto); 

var candidateInfoToAdd = new Candidate(); 
_mapper.Map<CandidateDto,Candidate>(candidateDto, candidateInfoToAdd); 
+0

흠,이 시도했지만 런타임에 동일한 원래 예외가 발생합니다. – keyle56