繁体   English   中英

将DTO映射到EF实体异常

[英]Mapping a DTO to an EF entity Exception

我有一个申请工作的申请。 申请人填写完所有信息并提交申请后,我要保存它。 当前,我有一个组件来保存应用程序的每个部分,因为它与数据模型(个人信息,可用性等)相关。 运行它时,出现一个异常,指出:

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

对于没有匹配的构造函数,请添加一个无参数ctor,添加可选参数,或映射所有构造函数参数

CandidateDto->候选人(目标成员列表)EmploymentApplication.Common.DataTransferObjects.CandidateDto-> EmploymentApplication.Entities.Candidate(目标成员列表)

未映射的属性:AddressId CandidateApplications CandidateAvailability CandidateEducations Education 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; }
}

谢谢您的帮助!

您定义了一些有关目标属性的配置以忽略它们,但是在尝试映射它们时,没有应传递给Map方法的目标实例。 因此,与忽略的属性无关。

因此,您应该更改它;

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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