c# - Entity Framework 和 Automapper 中的数据投影

标签 c# entity-framework linq-to-entities automapper

<分区>

我想使用 AutoMapper 构建一个 ViewModel(扁平化 - 数据投影)以用于 ASP.net MVC 应用。

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

当然,当我尝试上面的示例时,我收到 EF 错误“LINQ to Entities 无法识别方法 ... 方法,并且无法将此方法转换为存储表达式。”

我知道可以在 Automapper 发挥其魔力之前移动 .ToList(),但随后我从 Db 中获取所有字段(我只需要 20 个字段中的 3 个)

是否可以以干净的方式使用它。 Clean = 并非所有字段都从数据库中获取,而是仅获取 ViewModel 所需的字段。在Automapper中有可能吗?或者也许是其他图书馆? (无需手动操作;))

最佳答案

是的,这是很有可能的。看这里http://www.devtrends.co.uk/blog/stop-using-automapper-in-your-data-access-code

编辑:我最近发现 AutoMapper 中已经存在这方面的基础。为 AutoMapper.QueryableExtensions 添加一个 using 语句,您将获得一个名为 Project<>()

的 IQueryable 扩展

关于c# - Entity Framework 和 Automapper 中的数据投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391687/

相关文章:

c# - 针对接口(interface)和 Entity Framework 4.0 进行编程

.net - 存储库本身通常不经过测试?

linq - 使用 Linq to Entities (EF4.0) 输入和不输入

.net - LINQ to Entities 和 String.StartsWith 的问题

entity-framework - 使用Linq To Entities中的多列联接表

c# - 指定的包含路径无效。 EntityType 'SpiceShop.Models.Product' 未声明名称为 'Products' 的导航属性

c# - 突出显示 RichTextBox 中的一行

c# - 数据 GridView 错误 : Value' should be between 'minimum' and 'maximum'

c# - 角色管理器是否为自定义角色提供程序缓存数据,我可以清除此缓存吗?

c# - Entity Framework : Unable to call stored procedure,错误的代码生成