Automapper映射列表变为0 [英] Automapper mapping list becomes 0

查看:219
本文介绍了Automapper映射列表变为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我映射列表与Automapper另一个列表,但似乎我的项目不会被复制。

下面是我的code:

  VAR角色= userRepo.GetRoles(空).ToList();
Mapper.CreateMap<清单< D​​omain.Role>中列出<的角色和GT;>();
VAR mappedRole = Mapper.Map<名单,LT; D​​omain.Role>中列出<的角色和GT;>(角色); //计数为0,列表:(空
Mapper.AssertConfigurationIsValid();


  1. 没有例外被抛出。

  2. 所有的属性具有相同的名称。

Domain.Role

 公共类角色
{
    公众诠释角色ID {搞定;组; }
    公共字符串角色名{获得;组; }
    公开名单<使用者>用户{搞定;组; }
}

角色

 公共类角色
{
    公众诠释角色ID {搞定;组; }
    公共字符串角色名{获得;组; }
}


解决方案

不要只是类型之间创建列表和阵列之间的地图,

  Mapper.CreateMap< D​​omain.Role,角色>();

和则:

  VAR mappedRole = Mapper.Map<名单,LT; D​​omain.Role>中列出<的角色和GT;>(角色);

<一个href=\"http://automapper.$c$cplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home\">AutoMapper处理列表和数组自动的。

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).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();

  1. No exceptions were thrown.
  2. All properties has the same names.

Domain.Role

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public List<User> Users { get; set; }
}

Role

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
}

解决方案

Don't create maps between lists and array, only between the types:

Mapper.CreateMap<Domain.Role, Role>();

and then:

var mappedRole = Mapper.Map<List<Domain.Role>, List<Role>>(roles);

AutoMapper handles lists and arrays automatically.

这篇关于Automapper映射列表变为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆