簡體   English   中英

automapper 集合轉換創建基礎 class 屬性

[英]automapper collection convert create base class property

public class BaseEntity

{

​   Public Int Id{get;set;}

​   Public DateTime CreateTime{get;set;}

​   Public DateTime UpdateTime{get;set;}

}

public class EntityA:BaseEntity

{

​   Public string PropA{get;set;}

}

Public class BaseEntityDto

{

​   Public Int Id{get;set;}

}

public class EntityADto:BaseEntityDto

{

​   Public string PropA{get;set;}

}

我的自動映射器配置文件

CreateMap<EntityA, EntityDto>().ReverseMap();

我創建了一條記錄,可以包含Id,CreateTime,UpdateTime屬性

我需要通過 id 獲取實體然后更新實體

var entity = await _projectionEventRepository.FindAsync(w => w.Id == dto.Id);
var newentity = Mapper.Map<EntityADto, EntityA>(dto, entity); 
entity{Code:"100",CreateTime:"20222-1-30",UpdateTime:"2022-1-30"}
dto{Code:"101"}
ok result newentity{Code:"101",CreateTime:"20222-1-30",UpdateTime:"2022-1-30"}

但是現在我創建了一個不能包含 CreateTime,UpdateTime 屬性的列表,如何更改它。

我需要按 id 獲取列表,然后更新實體

var entitylist = await _projectionEventRoleRepository.FindAllAsync(w => w.ProjectionEventId == dto.Id);
var newentitylist = Mapper.Map<List<EntityADto>, List<EntityA>>(dtolist, entitylist);

目前,創建實體 dto 可以包含 Id、CreateTime 和 UpdateTime 屬性。 當我需要 map 一個集合時,dtolist 還可以自動創建 Id、CreateTime 和 UpdateTime 屬性。

您正在將實體列表傳遞給 .Map 調用,這可能會導致 .Map 調用返回您的 DTO 類型的集合。 試試這個代碼。

var newentitylist = Mapper.Map<List<EntityADto>, List<EntityA>>(dtolist);

請注意,我只是將源集合作為參數傳遞給.Map。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM