简体   繁体   中英

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

i need get entity by id then update entity

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.

i need getlist by id then update entity

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. When I need to map a collection, dtolist can also automatically create Id, CreateTime, and UpdateTime properties.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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