[英]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.