简体   繁体   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;}

}

my automapper profile我的自动映射器配置文件

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

I create a record that can contain Id, CreateTime,UpdateTime property我创建了一条记录,可以包含Id,CreateTime,UpdateTime属性

i need get entity by id then update entity我需要通过 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"}

but now I create a list that can't contain CreateTime,UpdateTime property, how to changed it.但是现在我创建了一个不能包含 CreateTime,UpdateTime 属性的列表,如何更改它。

i need getlist by id then update entity我需要按 id 获取列表,然后更新实体

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

At present, creating an entity dto can contain Id, CreateTime, and UpdateTime properties.目前,创建实体 dto 可以包含 Id、CreateTime 和 UpdateTime 属性。 When I need to map a collection, dtolist can also automatically create Id, CreateTime, and UpdateTime properties.当我需要 map 一个集合时,dtolist 还可以自动创建 Id、CreateTime 和 UpdateTime 属性。

You are passing in a list of entities to the.Map call, which may result in the.Map call returning a collection of your DTO type.您正在将实体列表传递给 .Map 调用,这可能会导致 .Map 调用返回您的 DTO 类型的集合。 Try this code instead.试试这个代码。

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

Note that I am only passing the source collection as an argument to.Map.请注意,我只是将源集合作为参数传递给.Map。

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

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