将DTO映射到EF实体异常

时间:2017-12-15 18:45:32

标签: c# entity-framework automapper dto

我有一份申请工作的申请。一旦申请人填写所有信息并提交申请,我想保存。目前,我有一个组件来保存与数据模型相关的每个应用程序(个人信息,可用性等)。当我运行它时,我得到一个异常,声明:

  

找到未映射的成员。查看下面的类型和成员。   添加自定义映射表达式,忽略,添加自定义解析程序或修改源/目标类型

     

对于没有匹配的构造函数,添加no-arg ctor,添加可选参数或映射所有构造函数参数

     

CandidateDto - >候选人(目的地会员名单)   EmploymentApplication.Common.DataTransferObjects.CandidateDto - > EmploymentApplication.Entities.Candidate(目的地成员列表)

     

未映射的属性:   AddressId   CandidateApplications   CandidateAvailabilities   CandidateEducations   CandidateEmploymentHistories   CandidateReferences   CandidateTeleLicenses

我试图在MapFrom语句中指定AddressId,对于其余的,它们只是EF导航属性,在映射初始化中我说要忽略。不幸的是,错误仍然存​​在,我现在不知道该怎么做。

以下是我的映射:

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

以下是实际发生映射的组件方法:

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

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

最后,这是候选人的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; }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您定义了一些有关目标属性的配置以忽略它们,但是当您尝试映射它时,没有应该传递Map方法的目标实例。所以,没有关于被忽略的属性。

所以,你应该改变它;

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

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