Pre-generating Entity Framework Views
Asked Answered
F

1

6

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?

Fer answered 23/6, 2015 at 22:23 Comment(0)
C
12

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.

Crassulaceous answered 24/6, 2015 at 4:17 Comment(10)
I included interactive pre-generated views and generated native images and my first call was reduced to 458 milliseconds. You're awesome.Fer
Hi Matthew, I'm working on an ASP.NET MVC application and I would test your solution. Where did you add Pawel's code? In Global.asax file? I defined the database context and edmx in an external dll. Thanks in advance.Afterworld
In addition: if I put the code in Global.asax I get back the following exception message: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an E...Afterworld
Pawel, now on Visual Studio 2017, can your library be safely used in Newest version of Entity Framework ( 6.1.3 ) ?Spoonful
@Spoonful - I think I might need to update the vsix otherwise it might not installCrassulaceous
Can you please update the vsix? I would like to download it done, and use on VS 2017. Can I automate the generation of the T4 output code on the BUILD time?Spoonful
I updated the extension in VS gallery. I don't think it makes much sense to run this tool on build. I think the best way is to run this tool only if the model changes.Crassulaceous
while using EFInteractiveViews, contaxt is assinged to InteractiveViews.SetViewCacheFactory in using block, but in my code db contaxt is created like this MyDbContext db = new MyDbContext(); at top of controller, how can i implement InteractiveViews. thanksDav
Have you measure if generating views makes any difference in your case? Because for smaller databases there is no difference so it may not be worth the hassle.Crassulaceous
can you disable the use of views via a config entry?Tupiguarani

© 2022 - 2024 — McMap. All rights reserved.