AutoMapper 3.1.1和Entity Framework 6.1代理对象 [英] AutoMapper 3.1.1 and Entity Framework 6.1 Proxy objects

查看:324
本文介绍了AutoMapper 3.1.1和Entity Framework 6.1代理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这一点已经被问过,但似乎我读过的解决方案,不有所作为至今。我使用实体框架6.1和3.1.1 AutoMapper。采取以下对象:公司 CompanyListItem ;我试试这个:

I realized this has been asked already, but the solutions I've read don't seem to make a difference so far. I'm using Entity Framework 6.1 and AutoMapper 3.1.1. Taking the following objects: Company and CompanyListItem; I try this:

Mapper.Configure<Company, CompanyListItem>();



好吧,当我尝试做的实际映射坠毁和异常燃烧,有没有映射定义。我知道这是因为实体框架创建的代理对象造成的。 One解决方案,到目前为止,我所看到的是调用映射方法的重载版本:

Well, when I try to do the actual mapping it crashed and burned with an exception that there's no mappings defined. I know this is caused because of the proxy objects created by Entity Framework. One solutions I've seen so far is to call an overloaded version of the Map method:

IQueryable<Company> companies = CompaniesService.FindCompanies();

Mapper.Map(companies, typeof(Company[]), typeof(CompanyListItem[]));

和它仍然无法正常工作,并骂我。

And it still doesn't work and yells at me.

我也看到了其他职位,这已定为2.2.1?这是不同的,或根本就退步?我会很感激的建议

I've also read other posts that this has been fixed as of 2.2.1? Is this different or did it regress? I'd appreciate suggestions.

推荐答案

您正在使用Automapper不当,您需要使用的QueryableExtensions 命名空间,所以你的代码应该是

You are using Automapper incorrectly, you need to use the QueryableExtensions namespace, so your code should be

IQueryable<Company> companies = CompaniesService.FindCompanies();

CompanyListItem[] results = companies.Project().To<CompanyListItem>().ToArray();



另外,如果你在那里要做到这一点,你仍然有其他的方式映射.MAP 错误的,它应该是

IQueryable<Company> companies = CompaniesService.FindCompanies();

CompanyListItem[] result = Mapper.Map<IEnumerable<Company>,CompanyListItem[]>(companies);



但我还是建议做。项目()为< T> ()方法,因为它会执行映射服务器端,而不是客户端,所以你可能有一个需要通过电线发送的数据量。

but I still recommend doing the .Project().To<T>() method as it will perform the mapping server side instead of client side so you potentially have less data that needs to be sent over the wire.

这篇关于AutoMapper 3.1.1和Entity Framework 6.1代理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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