Automapper mapping list becomes 0

后端 未结 3 1489
猫巷女王i
猫巷女王i 2020-12-19 04:09

I\'m mapping a list to another list with Automapper, but it seems that my items are not copied.

Here is my code:

var roles = userRepo.GetRoles(null).         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 04:31

    Avoid adding Collections in your mapper configuration. Make sure you add all the types(classes). I had errors when collections were not in the config, but that was because all types were not included. Bit misleading but that's where the problem lies. Point is, remove all collections from mapper config and add all classes only. Add collections when doing the actual transformation ie. call to mapper.Map.

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap();
                cfg.CreateMap().ReverseMap();
            });
    
            var mapper = config.CreateMapper();
            var domainPetOwners = mapper.Map>(repoPetOwners);
    

提交回复
热议问题