Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my EF Code First pregenerated views having no effect?

I have ~300 DbSets in my context and the first query after app load (a FirstOrDefault() where on an indexed field) takes ~40 seconds.

To improve this, I am attempting to use pregenerated views in EF 4.3.1 Code First using the T4 template here:

http://blog.3d-logic.com/2012/06/13/entity-framework-codefirst-view-generation-templates-on-visual-studio-code-gallery/

I compile it in, but I see no performance difference. I was hoping/assuming it would help the painful slow startup I am experiencing, but no luck.

Should it help? If not, what exactly are pregenerated views used for? And, is there anything I can do to improve startup time? Splitting my context up is painful to say the least.

like image 736
Scott Stafford Avatar asked Jul 18 '12 14:07

Scott Stafford


2 Answers

Certain ORM's like NHibernate and EF are simply slow to start up. Instead of trying to "fix" this slowness, I like to eliminate the problem by making sure IIS triggers this code whenever the app pool starts up. For this to fix the problem you must configure IIS to automatically start your app pool. This solution is only available to .NET 4 and IIS7.5 and newer.

You want to implement a class with IProcessHostPreloadClient which loads an ObjectContext and configure your application to use it by adding a serviceAutoStartProviders node to your web.config and setting startMode="AlwaysRunning" for your applications app pool.

Please refer to Scott Gu's blog for details.

like image 198
Gaute Løken Avatar answered Sep 20 '22 22:09

Gaute Løken


It turns out that it actually seems to search for the pregenerated views in the assembly where the first referenced entity is, not in the assembly where the DbContext is. See more discussion here: http://blog.3d-logic.com/2012/06/13/entity-framework-codefirst-view-generation-templates-on-visual-studio-code-gallery/#comment-76.

To work around this, I made up a new entity and put it in the context's assembly, and listed it as the first DbSet. Now it picks it up, and works well (except that this is ridiculous).

like image 34
Scott Stafford Avatar answered Sep 19 '22 22:09

Scott Stafford