列表方案的AutoMapper似乎仅重复映射列表中的第一个对象 [英] AutoMapper for a list scenario only seems to repeat mapping the first object in the list

查看:100
本文介绍了列表方案的AutoMapper似乎仅重复映射列表中的第一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MVC 3应用程序,并在使用AutoMapper在ViewModel和实体之间移动数据.我有一个场景,我需要在两个列表之间移动数据.由于某些奇怪的原因,AutoMapper似乎仅从源对象复制第一个对象,然后似乎将同一对象复制了n次到目标列表.例如,假设您有2个列表,源包含六个实体项,而目的地包含0个项,因为它们刚刚被实例化.将位置source [0]处的项目复制到目标,然后针对源列表中存在的相同数量的项目重复复制source [0],在这种情况下为6.原因.

I am developing an MVC 3 application and am using AutoMapper to move data between my ViewModels and my entities. I have a scenario where I need to move data between two lists. For some strange reason, AutoMapper seems to only copy the first object from the source object and then seems to copy the same object n times over to the destination list. For example, say you have 2 lists, source contains six entity items and destination contains 0 items as it was just instantiated. The item at position source[0] get copied over to the destination and then source[0] is copied repeatedly for the same number of items there are in the source List, in this case 6. I don't understand what could be the cause of this.

这是AutoMapper配置文件:

Here is the AutoMapper configuration file:

public static class AutoMapperConfigurator
{
    public static void Configure()
    {
        Mapper.CreateMap<User, UserModel>();
        Mapper.CreateMap<Posting, PostingModel>();
    }
}

这是Global.asax文件设置

Here is the Global.asax file setting

protected void Application_Start()
{
    AutoMapperConfigurator.Configure();
}

这是我调用Map方法的位置

Here is the location where I am calling the Map method

userSearchModel.UserList = Mapper.Map<IList<User>, IList<UserModel>>(userEntities);

推荐答案

对于其他有此问题的人,似乎该文档对我来说不起作用.一位同事提出了以下建议:

For anyone else with this problem, it appears as if the documentation was not working for me. A colleague made the following suggestion:

userSearchModel.UserList = UserEvent.Select(item => Mapper.Map<User, UserListModel>(item));

它就像一种魅力.

这篇关于列表方案的AutoMapper似乎仅重复映射列表中的第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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