Asp.net-Mvc
Automapper映射列表變為0
我正在使用 Automapper 將一個列表映射到另一個列表,但似乎我的項目沒有被複製。
這是我的程式碼:
var roles = userRepo.GetRoles(null).ToList(); Mapper.CreateMap < List<Domain.Role>, List<Role>>(); var mappedRole = Mapper.Map<List<Domain.Role>, List<Role>>(roles); //the count is 0, list empty :( Mapper.AssertConfigurationIsValid();
- 沒有拋出異常。
- 所有屬性都具有相同的名稱。
域.角色
public class Role { public int RoleId { get; set; } public string RoleName { get; set; } public List<User> Users { get; set; } }角色
public class Role { public int RoleId { get; set; } public string RoleName { get; set; } }
不要在列表和數組之間創建映射,只在類型之間創建:
Mapper.CreateMap<Domain.Role, Role>();進而:
var mappedRole = Mapper.Map<List<Domain.Role>, List<Role>>(roles);
就我而言,我正確配置了(父)類型映射,但我沒有為子記錄添加映射,因此:
class FieldGroup { string GroupName { get; set; } ... List<Field> fields { get; set; } }我不得不添加第二個映射:
cfg.CreateMap<FieldGroup, FieldGroupDTO>(); cfg.CreateMap<Field, FieldDTO>(); << was missing