问题
Having finally deployed to live a new site built with Entity Framework Code First and MySql, one nagging annoyance is the pause often experienced while view metadata is cached. This seems to happen far more frequently than the application pool is recycled. (?)
I guess that for a site which has a constant stream of traffic this might not be noticed often. For a new site with infrequent traffic it feels like every time I go to the site there's a 4-5 second delay in rendering the first page.
So I tracked down the following links:
http://msdn.microsoft.com/en-us/library/bb896240.aspx
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
The EF Power Tools CTP1 looked promising, apart from the fact that it seems to be broken for EF 4.3 and hasn't been updated for months. Also I'm not sure how that tool would fit in to our automated build process.
The OptimizeContext() or WriteEdmx() methods that EF Power Tools is using throw this error
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I initially assumed this might be MySql related but several others on the EF Power Tools Q&A section are reporting it.
So, in short, how do I pre-generate views for EF 4.3 code first, preferable in a way that can slot into an automated build process?
回答1:
Pawel answered my related question:
Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?
by building T4 templates:
http://blog.3d-logic.com/2012/05/28/entity-framework-code-first-and-pre-generated-views/
which also aren't perfect, but more usable than anything else that I've seen.
You could then do something like:
http://www.platinumbay.com/blogs/dotneticated/archive/2009/08/28/running-t4-templates-with-msbuild.aspx
to add it to the build process, but it's sort of slow and so you might want to just leave it as a(nother) manual step in updating the EF model.
Really, they need a warning that says, DON'T USE EF WITH > 500 TABLES, it becomes increasingly annoying to use.
回答2:
I have encountered this same error when I am running VS2010, EF v4.3.1 and Entity Framework Power Tools Beta 2. The solution for me was to run Visual Studio as admin. (Done by right clicking the Icon and select "run as administrator" from the context menu.) At that point I was able to use the "generate views" option in EFPT.
来源:https://stackoverflow.com/questions/9888789/pre-generate-views-for-ef-code-first