简体   繁体   English

将DTO映射到EF实体异常

[英]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 对于没有匹配的构造函数,请添加一个无参数ctor,添加可选参数,或映射所有构造函数参数

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

Unmapped properties: AddressId CandidateApplications CandidateAvailabilities CandidateEducations CandidateEmploymentHistories CandidateReferences CandidateTeleLicenses 未映射的属性:AddressId CandidateApplications CandidateAvailability CandidateEducations Education 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. 我试图在MapFrom语句中指定AddressId,其余的它们只是我说过在映射初始化中忽略的EF导航属性。 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: 这是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: 最后,这是候选人的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; }
}

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. 您定义了一些有关目标属性的配置以忽略它们,但是在尝试映射它们时,没有应传递给Map方法的目标实例。 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);

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

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