[英]Map a list of objects manually using automapper
我正在使用 automapper 將一個復雜對象映射到另一個對象,其中源對象有一個對象列表,其屬性與目標中的對象列表不匹配。
因此,我使用 Linq 手動瀏覽源中的列表並將其映射到目標對象。
問題是,目標對象是一個數據庫對象:
源對象:
public class AutomationDetailsResponse
{
public Guid ServiceId { get; set; }
public int EngagementId { get; set; }
public string ServiceRequestName { get; set; }
public Guid ServiceRequestGUID { get; set; }
public string OfficeId { get; set; }
public string CountryId { get; set; }
public string EngagementCode { get; set; }
public string CanvasDocumentUri { get; set; }
public string CanvasAudienceUri { get; set; }
public List<RequestFile> InputRequestFiles { get; set; }
public List<RequestFile> OutputRequestFiles { get; set; }
public string Status { get; set; }
public string RequestInitiatedByName { get; set; }
public string RequestInitiatedBy { get; set; }
public int DataCenterId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime LastUpdatedAt { get; set; }
public List<ActivityFeed> ActivityFeeds { get; set; }
public List<Guid> TaskIds { get; set; }
[JsonProperty("Id")]
public int CanvasRequestId { get; set; }
}
public class RequestFile
{
public Guid Id { get; set; }
public int FileSequence { get; set; }
public Guid GroupId { get; set; }
public string GroupName { get; set; }
public string FileType { get; set; }
public string FileName { get; set; }
public int FileSize { get; set; }
public Guid DocumentId { get; set; }
public DateTime CreatedAt { get; set; }
}
public class ActivityFeed
{
public int CreatedById { get; set; }
public DateTime CreatedAt { get; set; }
public string Description { get; set; }
public int ActivityType { get; set; }
public string UserName { get; set; }
public int Id { get; set; }
}
目標對象:
[Table(nameof(AutomationRequest), Schema = Schemas.SAH)]
public class AutomationRequest : EntityBase, ICreatedDate, IModifiedDate
{
[Required]
public int CanvasEnvironmentId { get; set; }
[Required]
public Guid AppInstanceId { get; set; }
public Guid CanvasRequestGUID { get; set; }
public int CanvasRequestId { get; set; }
public int DownstreamStatusId { get; set; }
[Required]
public int CanvasStatusId { get; set; }
[Required]
public Guid ServiceCatalogId { get; set; }
[Required]
public string RequestName { get; set; }
[Required]
[MaxLength(255)]
public string EngagementId { get; set; }
[Required]
public int CountryId { get; set; }
[Required]
public int DataCenterId { get; set; }
public bool IsDeletedFromSAH { get; set; }
public bool IsDeletedFromCAH { get; set; }
[MaxLength(500)]
public string CreatedByEmail { get; set; }
[MaxLength(500)]
public string CreatedByName { get; set; }
[Required]
public DateTime CreatedDate { get; set; }
[MaxLength(38)]
public string ModifiedBy { get; set; }
[MaxLength(50)]
public string OriginatedFrom { get; set; }
[Required]
public DateTime ModifiedDate { get; set; }
public string NavigationUrl { get; set; }
public int? Version { get; set; }
[ForeignKey(nameof(CanvasEnvironmentId))]
public virtual Environment CanvasEnvironment { get; set; }
[ForeignKey(nameof(CountryId))]
public virtual Country Country { get; set; }
[ForeignKey(nameof(ServiceCatalogId))]
public virtual ServiceCatalog ServiceCatalog { get; set; }
[ForeignKey(nameof(CanvasStatusId))]
public virtual AutomationRequestStatus CanvasStatus { get; set; }
public virtual ICollection<AutomationRequestInputFile> InputFiles { get; set; }
public virtual ICollection<AutomationRequestOutputFile> OutputFiles { get; set; }
public virtual ICollection<AutomationRequestTask> Tasks { get; set; }
}
[Table(nameof(AutomationRequestInputFile), Schema = Schemas.SAH)]
public class AutomationRequestInputFile : EntityBase, ICreatedDate, IModifiedDate
{
[Required]
public Guid CanvasGroupId { get; set; }
public Guid CanvasDocumentId { get; set; }
[MaxLength(255)]
public string FileName { get; set; }
[MaxLength(38)]
public string CreatedBy { get; set; }
[Required]
public DateTime CreatedDate { get; set; }
[MaxLength(38)]
public string ModifiedBy { get; set; }
[Required]
public DateTime ModifiedDate { get; set; }
[Required]
public Guid AutomationRequestId { get; set; }
public string FileType { get; set; }
[ForeignKey(nameof(AutomationRequestId))]
public virtual AutomationRequest AutomationRequest { get; set; }
}
public class EntityBase
{
public EntityBase()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
}
這是映射配置:
CreateMap<AutomationDetailsResponse, AutomationRequest>()
.ForMember(dst => dst.Id, opt => opt.Ignore())
.ForMember(
dst => dst.CanvasRequestGUID,
opt => opt.MapFrom(src => src.ServiceRequestGUID))
.ForMember(
dst => dst.RequestName,
opt => opt.MapFrom(src => src.ServiceRequestName))
.ForMember(
dst => dst.InputFiles,
opt => opt.MapFrom(src => src.InputRequestFiles.Select(x => new AutomationRequestInputFile { CanvasGroupId = x.GroupId, CanvasDocumentId = x.DocumentId, FileName = x.FileName, FileType = x.FileType })))
.ForMember(
dst => dst.OutputFiles,
opt => opt.MapFrom(src => src.OutputRequestFiles.Select(x => new AutomationRequestOutputFile { CanvasDocumentId = x.DocumentId,FileName = x.FileName, FileType = x.FileType, FileSize = x.FileSize, CreatedDate = x.CreatedAt })))
.ForMember(
dst => dst.CreatedByName,
opt => opt.MapFrom(src => src.RequestInitiatedByName))
.ForMember(
dst => dst.CreatedDate,
opt => opt.MapFrom(src => src.CreatedAt))
.ForMember(
dst => dst.ModifiedDate,
opt => opt.MapFrom(src => src.LastUpdatedAt))
.ForMember(
dst => dst.Tasks,
opt => opt.MapFrom(src => src.TaskIds.Select(x => new AutomationRequestTask { CanvasId = x })))
;
映射有效。 問題是這樣的:
var automation = await _dbContext.AutomationRequests.FirstOrDefaultAsync(x => x.CanvasRequestGUID == automationDetails.ServiceRequestGUID);
_mapper.Map(automationDetails, automation);
_dbContext.Update(automation);
await _dbContext.SaveChangesAsync();
更新失敗,因為在映射配置中,我們創建了一個新實例 AutomationRequestInputFile,它觸發了 EntityBase 中的構造函數,創建了一個新的 Id。 當 EF Core 嘗試更新該行時,它找不到該記錄,因為 Id 已更改。
我一直試圖解決這個問題 1 天了,但沒有取得任何進展。
任何幫助表示贊賞。
謝謝。
您沒有正確使用 AutoMapper,因為您仍在手動構建AutomationRequestInputFile
對象(通過使用 LINQ .Select())。
解決方案是將RequestFile
的第二個映射添加到AutomationRequestInputFile
並刪除“InputFiles”屬性的 MapFrom 配置中的 Select。
這是一個有效的 .NET Fiddle(您的問題中未提供的屬性和類型已被省略): https : //dotnetfiddle.net/9qizfk
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.