How to pre-compile my EF Views


Here is the story : I had a huge MVC 2 website using Entity Framework 4.0 and a few days ago, I decided to upgrade MVC 2 => MVC 4 and EF4 => EF6 and .NET Framework 4.5.

I did that on a project containing about 150 controllers and more than 300 entities.

Now the project is running, but not as fast as I thought it would be.

I'm using Entity Framework 6.0.2 with EF 5.x DBContext Generator T4. I used the DB Context generator because with EF4 I did Database First.

In the past I used this command line to pre-compile my views.

"%windir%\Microsoft.NET\Framework\v4.0.30319\EdmGen.exe" /nologo /language:CSharp /mode:ViewGeneration "/inssdl:$(TargetDir)Models/DBModel.ssdl" "/incsdl:$(TargetDir)Models/DBModel.csdl" "/inmsl:$(TargetDir)Models/DBModel.msl" "/outviews:$(ProjectDir)Models/DBModel.Views.cs"

XCOPY "$(TargetDir)Models/DBModel.csdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)Models/DBModel.ssdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)Models/DBModel.msl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)Models/SiteWeb.csdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)Models/SiteWeb.ssdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)Models/SiteWeb.msl" "$(ProjectDir)" /R /Y

Now, how do I do the same thing with EF6?

Accepted Answer

You cannot use EdmGen to generate views for EF6. One way to go is to use PowerTools. I also created a number of T4 templates (available on VS Gallery) for generating views and one of them is for EF6. Not sure how helpful it will be for you since the EF6 one is targeted at CodeFirst. Finally, I recently published a package on NuGet which allows generating views on the fly and saving them for later - see this for more details.

btw. Using 5.x DBContext generator feels wrong with EF6 feels wrong. EF Tooling that ships with VS2013 (and a standalone version available for VS2012) uses EF6 templates to generate the code so I am not sure why you use version for EF5 (and am kind of wondering how it even compiles given this)

Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why