在实体框架和Automapper数据投影 [英] Data projection in Entity Framework and Automapper

查看:337
本文介绍了在实体框架和Automapper数据投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用AutoMapper构造一个视图模型。(压扁 - 数据投影),用于在ASP.net MVC应用程序使用

I want to use AutoMapper to construct a ViewModel (flattening - data projection) for use in an ASP.net MVC app.

var tmp = from x in db.Mailings select Mapper.Map<Mailing, MailingViewModel>(x);
return View(tmp.ToList());



当然,当我尝试上面的例子,我得到的EF错误LINQ到实体不识别方法...方法,而这种方法不能被翻译成店表情。

Of course, when I try the sample above, I get the EF error "LINQ to Entities does not recognize the method ... method, and this method cannot be translated into a store expression."

我知道这是可能移动.ToList()的Automapper不前它的魔力,但后来我获取从数据库中的所有字段(我只需要3 20场)

I know it's possible to move the .ToList() before the Automapper does its magic, but then I fetch all the fields from the Db (and I only need 3 of 20 fields)

是否有可能使用一个干净的方式。清洁=并非所有领域都获取从数据库中,但必要的视图模型只等领域。是否有可能在Automapper?或者可能是其他图书馆? (没有做手工;))

Is it possible to use that in a clean way. Clean = Not all the fields are fetched from the DB, but only the fields necessary for the ViewModel. Is it possible in Automapper? Or perhaps an other library? (without doing it manually ;) )

推荐答案

是的,这是很有可能的。看到这里 http://www.devtrends.co.uk/

Yes this is very possible. See here http://www.devtrends.co.uk/blog/stop-using-automapper-in-your-data-access-code

编辑博客/使用-automapper - 在你的数据访问代码停:我最近发现,依据这个已经存在在AutoMapper。添加一个using语句AutoMapper.QueryableExtensions并为您提供了一个名为Project< IQueryable的扩展;>()

I've recently found that the basis to this already exists in AutoMapper. add a using statement for AutoMapper.QueryableExtensions and you are provided with an IQueryable extension called Project<>()

这篇关于在实体框架和Automapper数据投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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