Entity Framework是否自动缓存ModelBuilder模型? [英] Does Entity Framework automatically cache the ModelBuilder model?

查看:87
本文介绍了Entity Framework是否自动缓存ModelBuilder模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架开发ASP MVC应用程序。我正在考虑编写代码以缓存由ModelBuilder返回的对象(由几个来源推荐),但后来我在Scott Gu的博客中遇到过:



OnModelCreating方法将在运行的应用程序中首次使用NerdDinners类时被称为ModelBuilder对象作为参数,ModelBuilder对象可用于自定义模型对象的数据库持久性映射规则。我们将在下面的例子中查看如何执行此操作。



EF只在正在运行的应用程序中调用OnModelCreating方法一次 - 然后自动缓存ModelBuilder结果。这避免了每次NerdDinners类被实例化时模型创建的性能影响,并且意味着您不必编写任何自定义缓存逻辑,以在应用程序中获得出色的性能。


$ b $这是否意味着EF会自动缓存ModelBuilder对象,而且我不必编写代码来执行,或者只有在OnModelCreating方法被覆盖的情况下才可以这样做,或者... ??

解决方案

实体框架博客关于EF 4中的性能改进

模型缓存




中涉及到一些成本,发现模型,处理Data
注释和应用流畅的API
配置。为了避免每次派生DbContext
i,都会产生这个
的成本在第一次初始化期间,模型被缓存

缓存模型然后每次
重新使用相同的派生上下文是
在同一个AppDomain中构造。
模型缓存可以由
关闭,将
的OnModelCreating方法设置为ModelBuilder上的CacheForContextType
属性为false。


所以对于Entity Framework 4.0,答案是肯定的。


I am developing an ASP MVC application using Entity Framework. I was thinking of writing code to cache the object returned by ModelBuilder (as is recommended by several sources), but then I ran into this on Scott Gu's blog:

"The OnModelCreating method above will be called the first time our NerdDinners class is used within a running application, and it is passed a "ModelBuilder" object as an argument. The ModelBuilder object can be used to customize the database persistence mapping rules of our model objects. We’ll look at some examples of how to do this below.

"EF only calls the "OnModelCreating" method once within a running application – and then automatically caches the ModelBuilder results. This avoids the performance hit of model creation each time a NerdDinners class is instantiated, and means that you don’t have to write any custom caching logic to get great performance within your applications."

Does this mean that EF automatically caches the ModelBuilder object, and I don't have to write code to do it, or is this something that is only done if the OnModelCreating method is overridden, or ... ??

解决方案

From the Entity Framework Blog regarding performance improvements in EF 4

Model Caching

There is some cost involved in discovering the model, processing Data Annotations and applying fluent API configuration. To avoid incurring this cost every time a derived DbContext is instantiated the model is cached during the first initialization. The cached model is then re-used each time the same derived context is constructed in the same AppDomain. Model caching can be turned off by setting the CacheForContextType property on ModelBuilder to ‘false’ in the OnModelCreating method.

So the answer is yes for Entity Framework 4.0

这篇关于Entity Framework是否自动缓存ModelBuilder模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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