Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-generating Entity Framework Views

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?

like image 254
Matthew Meppiel Avatar asked Jun 23 '15 22:06

Matthew Meppiel


1 Answers

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.

like image 101
Pawel Avatar answered Sep 29 '22 08:09

Pawel