问题
In desperate need of help.
I'm using entity framework code first and trying to follow Julie Lerman's suggestion about breaking out the context from the domain classes and I'm unable to get entity framework to recognize the compiled views. I've tried many things but the best so far is that I can get the constructor of the compiled views to be called but it still takes about 15 mins for the 1st query (about 329 entities and 600+ views created). This makes me think it is still having to create the views on the 1st query.
Steps
Using an existing database and a "Model project" I ran the Entity Power Tools Beta 3 to "Reverse Engineer Code First" the database. This created the context, domain classes and mapping (fluent) classes. FYI, I do this because I have modified the T4 template slightly to match our coding standard.
Using Julie Lerman's recommendations from her Plural Site training videos, I broke out the domain classes, contexts and mapping classes into 3 separate projects (context, domain classes, mappings). I copied the files from the model project into these 3 projects respectively and then changed the namespace to correspond to the project name and location. The results and namespaces for the 4 projects: "MyCompany.DataContext", "MyCompany.DomainClasses", "MyCompany.DomainMappings", "MyCompany.Model".
Using the "model project" and the code generated from the Entity Power Tools I selected the context file created and pressed the right mouse button to display the context menu whereby I could select "Generate Views". When I do this it gives me an error "Sequence contains no matching element". (Problem #1)
Ok, so that didn't work so then I added a T4 template by "Pawel Kadluczka" to the "MyCompany.DataContext" project for generating views and ran that custom tool and success, the views were created without error. However when I run my test application to do a basic query, the 1st query takes 15 mins and the constructor "ViewsForBaseEntitySets1B04A...0F8A()" of the generated views does not get called.
Ok, so that didn't work either so I added the "views" file created by the T4 template into the "MyCompany.DomainClasses" project so that it will get compiled with the classes and ran the test application again. This time, the 1st query still took 15 mins but within about 20 seconds of starting the constructor "ViewsForBaseEntitySets1B04A...0F8A()" was called. This is progress but no banana. This makes me think that the views must be in the same assembly as the domain classes instead of the context. Question: Is this a correct assumption? Also, since it still took 15 mins, it was like Entity Framework saw the views but didn't like them so it recreated them again. Possible?
Now what? I'm at a loss and have been beating my head against the wall. How do I create precompiled views with multiple contexts where I have the projects broken out? Where do the views go? Is there a namespace rule that I'm not following? What happens when I have 2 or more contexts?
Please help. BC
回答1:
Resolved. It appears that regardless of the context's assembly, the precompiled views must be in the same assembly as the domain classes.
Also, the ultimate cause of my problem was that I had another context that was actually being used instead of the one that was being instantiated. I didn't realize that both contexts were being instantiated but the 2nd one was what was actually being called, and since I hadn't created compiled views for it, it was taking a long time each time the test application would run. Now the initial query is 18 seconds instead of 15 minutes because I'm using the 2nd context with the newly compiled views.
Now, I have 2 context projects (MyCompany.Context, MyCompany.SystemContext) and a MyCompany.DomainClasses project, MyCompany.Model project and a MyCompany.DomainMappings project. I use the T4 script to generate the views in both context projects and I included the view files from the MyCompany.DomainClasses using a copied link so that when it regenerates, it also updates the Domain Classes file. This allows the Entity Framework to detect the compiled views which reduces the startup time for the 1st query.
来源:https://stackoverflow.com/questions/16695197/which-assembly-should-i-place-the-compiled-views-for-entity-framework-code-first