views:

37

answers:

1

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 ... ??

+2  A: 

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

Josh
If I read that correctly, he's saying that the MODEL is cached but not the CONTEXT, even though the caching occurs whenever the context is instantiated. Right? It would not make sense to cache the DbContext object, as that changes throughout the session. That, I am planning to store in the HttpContext.Current object.
Cynthia
I would be careful about caching the entire context in the global cache. You could run into all kinds of synchronization issues if you are not careful. Why are you looking to do this?
Josh
I am not caching the context in the global cache. I am planning to cache it in the HttpContext.Current object, as I stated. It's the MODEL I was going to cache -- as in ModelBuilder model = new ModelBuilder(). Then you need to use the model to create the context -- model.CreateObjectContext<type>(...) etc. Right? (or CreateDbContext)
Cynthia
I'm taking my cues from the ADO.NET developer blog - http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4codefirstwalkthrough.aspx
Cynthia
As in the following code: var builder = new ModelBuilder(); builder.Configurations.Add(new BookConfiguration()); builder.Entity<Person>(); builder.Entity<Publisher>().Property(p => p.Name).IsRequired().HasMaxLength(50); var model = builder.CreateModel();
Cynthia