【问题标题】:EntityCollection<T> ToArray() extension method not working in Global.asax.csEntityCollection<T> ToArray() 扩展方法在 Global.asax.cs 中不起作用
【发布时间】:2012-07-28 17:57:15
【问题描述】:

我正在尝试将我的 Automapper Entity -> ViewModel 映射定义从我的控制器之一移动到我的 MVC 应用程序的 OnApplicationStarted() 方法。当我复制它时,我的实体的 EntityCollection 属性将无法访问其 ToArray() 扩展方法。当我尝试编译时,我收到一条错误消息,告诉我没有与其签名匹配的方法或扩展方法。

代码:

    protected override void OnApplicationStarted()
    {
        // some Ninject setup code

        Mapper.CreateMap<Game, AdminGameViewModel>()
            .BeforeMap((s, d) =>
            {
                int platCount = s.Platforms.Count;
                var plats = s.Platforms.ToArray(); // <-- line in question
                d.PlatformIDs = new int[platCount];

                for (int i = 0; i < platCount; ++i)
                {
                    d.PlatformIDs[i] = plats[i].ID;
                }
            })
            .ForMember(dest => dest.Pros, opt => opt.MapFrom(src => src.Pros.Split(new char[] { '|' })))
            .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => src.Cons.Split(new char[] { '|' })))
            .ForMember(dest => dest.PlatformIDs, opt => opt.Ignore());
    }

同样,这段代码是直接从我的控制器复制和粘贴的,在那里它可以正常编译和运行。我已尝试强制转换为 IEnumerable,但这也无法让我访问该方法。

【问题讨论】:

  • 添加用于 System.Data.Linq
  • s.Platforms的类型是什么?
  • @k0stya,你应该写下你的评论作为答案,这样我就可以投票并将其标记为已解决。

标签: c#-4.0 asp.net-mvc-2 entity-framework-4 automapper


【解决方案1】:

使用添加以下内容。

using System.Data.Linq;

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多