首先使用EF 5代码将视图映射到实体 [英] Mapping View to Entity using EF 5 Code First

查看:315
本文介绍了首先使用EF 5代码将视图映射到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用EF 5开发了一个非常不错的网络应用程序,并且首先编写了代码.但是在运行基准测试时,我发现性能不如我想要的好.进一步看,我发现EF生成的所有查询都与Select * From类似,而这并不是最佳实践.

I have developed a quite nice web-app using EF 5 and code first. But while running benchmarks I found that the performance was not as good as I wanted... looking further I kinda figured out that all the queries that EF generates are similar to Select * From and that is not best practise.

在此处阅读此答案使用EF从数据库中选择特定列代码优先我了解我可以生成一个视图并将其映射到实体.我的问题是如何首先使用EF 5代码将视图映射到实体,反之亦然?

Reading this answer here Select Specific Columns from Database using EF Code First I understood that I could generate a view and map it to a entity. My question is how do I map a view to a entity or vice-versa using EF 5 code first?

我问这个问题的原因是:我有一张很宽的桌子,可以在其中按名称执行初步搜索"搜索项,然后在一种情况下返回其余信息……在另一种情况下,我有一个大表,在大多数情况下,我只使用标题和说明,而不使用LOB列...在所有情况下,我都是从数据库中获取某些信息,而不是在使用...

The reason I'm asking this is: I have a very wide table on which I perform "preliminar search" search items by name and then go back for the rest of it on one case... in another I have a big table and most of the time I only use the Title and Description and not the LOB column... in all thouse cases Im getting something from the database Im not using...

因此,如果我确实可以将视图映射到实体,反之亦然,则可以在主干和应用程序层之间节省大量带宽...

So if I could indeed map a view to a entity or vice-versa I could save alot of bandwith between backbone and application tier...

推荐答案

您所谈论的不是同一件事-即不是确切的答案-而是通过EF所谓的视图"来解决性能问题.

It's not the same thing you're talking about - i.e. not an exact answer - but it's addressing performance, via what EF calls 'views'.

我建议您尝试使用 EF电动工具-和生成视图".

I'd suggest you try out the EF Power Tools - and 'Generate Views'.

通过运行-将"views"文件添加到项目中-这是一个.cs文件-并增强了核心EF性能(这是EF功能,不是代码优先的EF功能,而是功能强大的-工具,我们现在也可以将其与代码优先结合使用).

By running that - the 'views' file is added to the project - which is a .cs one - and that enhances the core EF performance (this is an EF feature, not the code-first - but with power-tools we can now use it with code-first as well).

它没有添加'Db视图'-但据我所知-它通过预先分析和代码生成SQL模板而起作用.

It doesn't add the 'Db views' - but as far as I can tell - it works by pre-analyzing and code-generating the SQL templates.

在实体框架可以针对概念执行查询之前 对数据源进行建模或保存更改,它必须生成一组 本地查询视图以访问数据库.意见是 每个应用程序域缓存的元数据.如果您创建 同一应用程序域中的多个对象上下文实例,它们 将重用缓存的元数据中的视图,而不是重新生成 他们.因为视图生成是整体的重要组成部分 执行单个查询的成本,实体框架使您能够 预先生成这些视图,并将其包含在已编译的项目中.为了 有关更多信息,请参阅性能注意事项(实体框架)."
http://msdn.microsoft.com/en-us/library/bb896240.aspx

"Before the Entity Framework can execute a query against a conceptual model or save changes to the data source, it must generate a set of local query views to access the database. The views are part of the metadata which is cached per application domain. If you create multiple object context instances in the same application domain, they will reuse views from the cached metadata rather than regenerating them. Because view generation is a significant part of the overall cost of executing a single query, the Entity Framework enables you to pre-generate these views and include them in the compiled project. For more information, see Performance Considerations (Entity Framework)."
http://msdn.microsoft.com/en-us/library/bb896240.aspx

我可以感觉"到性能上的提升.

I could 'feel' a boost in performance.

注意:
它有几个问题-您可能会在首次运行时遇到一些例外情况:

Notes:
There are couple issues with it - and you might get some exceptions running it the first time:

  • 确保您的课程是文件中唯一的上下文(需要第一个),
  • 我不得不将项目移出解决方案目录" (这是我从Power-shell控制台中学到的技巧-要求相同)
  • Make sure your class is the only context in the file (it takes the first one),
  • I had to move the project out of a 'solution dir' (that is a trick I learned from power-shell console - which required the same)

此外,任何其他尝试以真实"视图手动调整" Db的尝试-都是徒劳的,因为它与ORM紧密集成(您需要一个以上的对象-并匹配调用等) ).

Also, any other attempts to manually 'tweak' the Db with the 'real' views - would be futile I think, as it isn't closely integrated w/ the ORM (you need more then one - and matching calls etc.).

这篇关于首先使用EF 5代码将视图映射到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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