Pre-generating Entity Framework Views c# ef-database-first entity-framework


I'm working on an MVC 5 application that uses Entity Framework 6.1.3. I'm trying to optimize the first call, which usually takes 1-4 seconds, by pre-generating the views. I understand why this is happening, but I feel like I'm missing an important step somewhere.

My current test runs the same function five times to pulls a collection of objects from my DB using EF. When I run this with 'Embed in Output Directory' set in my edmx I get the following results:


  • First EF Call: 2617 milliseconds
  • Second EF Call: 19 milliseconds
  • Third EF Call: 19 milliseconds
  • Fourth EF Call: 17 milliseconds
  • Fifth EF Call: 20 milliseconds


I expect this since EF is generating the local views to access the database...

When I switch to 'Copy to Output Directory', double check that my files are being copied to the bin folder and my connection string has been updated, and then run the same test I get similar results:


  • First EF Call: 2546 milliseconds
  • Second EF Call: 19 milliseconds
  • Third EF Call: 18 milliseconds
  • Fourth EF Call: 18 milliseconds
  • Fifth EF Call: 21 milliseconds


No change in the first call, which is strange. I added EF Power Tools, right-clicked on my edmx => Entity Framework => Generate Views. A 'Model1.View.cs' file was created with string for each of my tables/views, so I felt like I was on the right path. When I ran the test again I got the same results. Putting a breakpoint in each method of the newly generated 'Model1.View.cs' file shows that it's never being hit.

Am I missing an important step here? How do I tell EF to use the file that was created by EF Power Tools, or how do I make the Copy to Output Directory work as I've read online?

6/24/2015 5:04:46 AM

Accepted Answer

As far as I remember EF Power Tools was not updated to support EF6. If you are sure this is view generation that is causing the issue you could either use the view generation T4 template or the interactive pre-gegnerated views (yes, I am the creator of both). You can find more details on how to use the T4 template in this post. Interactive Pre-Generated views are described here.

However, view generation in EF6 was greatly improved and is only a problem for bigger models or when complex hierarchies are involved. I assume the bottleneck you are seeing is not caused by view generation but by model building. Take a look at this post by EF team that talks a little bit about improving startup performance by generating native images. You may also want to take a look at Performance Consideration.

6/24/2015 4:17:48 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow