简体   繁体   中英

Mapping a DTO to an EF entity Exception

I have an application to apply for jobs. Once an applicant fills out all of the information and submits the application, I want to save it. Currently I have a component to save each piece of the application as it relates to the data model (personal info, availability, etc). When I run it, I get an exception that states:

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

CandidateDto -> Candidate (Destination member list) EmploymentApplication.Common.DataTransferObjects.CandidateDto -> EmploymentApplication.Entities.Candidate (Destination member list)

Unmapped properties: AddressId CandidateApplications CandidateAvailabilities CandidateEducations CandidateEmploymentHistories CandidateReferences CandidateTeleLicenses

I have tried to specify the AddressId in a MapFrom statement and for the rest, they are just EF navigation properties that in the mapping initialization I said to ignore. Unfortunately, the error persists and I don't know what to do now.

Here is a look at my mappings:

        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())
        );

Here is the component method where the Mapping actually occurs:

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

Here is the DTO:

    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; }
}

And lastly, here is EF class for a candidate:

 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; }
}

Thanks for the help!

You defined some configurations about destination properties to ignore them but when you are trying to map it, there is no destination instance which should be passed the Map method. So, there is no matter about ignored properties.

So, you should change it;

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM