Pre-Generating Entity Framework Views with Embedded Metadata

Problem: When you are using embedded metadata in your Entity Framework project, the MSDN topic for How to: Pre-Generate Views to Improve Query Performance (Entity Framework) comes up short.

Solution: This posting shows a simple (if tedious) technique for working with both embedded metadata and the pre-generated views.

Step 1: Edit the XML of your EDMX file, down near the bottom, locate the Designer tag:

<edmx:Designer xmlns="">
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />

Change this by adding an XML Comment to the DesignerInfoPropertySet tag:

<edmx:Designer xmlns="">
        <!--DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /-->
        <!--DesignerProperty Name="MetadataArtifactProcessing" Value="CopyToOutputDirectory" /-->
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />

Step 2: Edit the Project Properties | Build Events. Add a pre-build event:

rem "%windir%\Microsoft.NET\Framework\v3.5\EdmGen.exe" /nologo /language:CSharp /mode:ViewGeneration "/inssdl:$(TargetDir)ServerModel.ssdl" "/incsdl:$(TargetDir)ServerModel.csdl" "/inmsl:$(TargetDir)ServerModel.msl" "/outviews:$(ProjectDir)ServerModel.Views.cs"

Now, at this point the projects are relatively unchanged, you’ve added a comment to the EDMX file and a comment to the Build Events. During active Entity Model changes you should leave these comments in place. Once the model is stable (more-or-less), you can pre-generate the views.

To Pre-generate the Views:
Step 1: Edit the EDMX file and change the DesignerProporty tag to CopyToOutputDirectory:

<DesignerProperty Name="MetadataArtifactProcessing" Value="CopyToOutputDirectory" />

Step 2: Uncomment the build event:

"%windir%\Microsoft.NET\Framework\v3.5\EdmGen.exe" ...

Step 3: Build the Project. The build will now generate the ServerModel.Views.cs file.

Step 4: Add the (now) Existing Item “ServerModel.Views.cs” into your Entity Model project.

Step 5: Reverse the changes made in Step 1 and Step 2. (Changing CopyToOutputDirectory back to EmbedInOutputAssembly, and adding REM back in front of the build event.

As I said, it’s tedious, however, it works. Smarter folks than me can probably automate it. (If you do, I’d love to know how.)

Microsoft MVC – First Attempt

I finally found some useful web pages that explained MVC in a way I could understand.

This is where I started. Scott Guthrie is one of several great Microsoft bloggers. I tried this code, but it is badly out of date. But the descriptions are excellent and most instructive.
ASP.NET MVC Framework (Part 1)

Phil Haack had an interesting summary of MVC/MVP:
Everything You Wanted To Know About MVC and MVP But Were Afraid To Ask

Dino Esposito also has some interesting comments that help illuminate the MVC topic:
Architectural View of the ASP.NET MVC Framework.

The MVC tutorials were useful too, once I understood what these guys were all talking about. I’ve referred to these tutorials several times.
ASP.NET MVC Tutorials

Here’s the technical docs on MVC. They’re a little lean, but still useful.
ASP.NET Model-View-Controller Applications

Lastly, a couple of the Telerik bloggers are great too.
Atanas Korchev has a working sample app that uses the RadGrid and other RadControls to display data in a sample MVC forum application. His blog entry has the download at:
ASP.NET Ajax Controls in ASP.NET MVC – Announcing the sample application
Vladimir Enchev also has some interesting posts on MVC. Particularly this one:
Telerik RadControls in Microsoft ASP.NET MVC

So, from all of this, I was able to piece together a working MVC sample.

It doesn’t do much, just retrieves a few rows from a table and displays them in a Telerik RadGrid. There’s even a unit test to make sure the Controller returns the rows from the model that I expected it to. I’ll post some code on this soon.

Seasons Change… Greener Pastures?

After spending several months figuring out how to automate the build of a Java app in using Maven, CruiseControl and myriad other tools, I took a job where I can return to working with Microsoft technology. However, little did I know, the greener grass wasn’t mowed and disguised the morass that is web development anyway. So, instead of learning Java, I have had to learn ASP.Net, Javascript and AJAX, Telerik RadControls for Ajax and more. It turns out that with ViewState, Session State and a Stateless environment, everything gets confused. Add in Server side, Client side, Postbacks, AsyncCallBacks and it’s a royal mess.

Having recently having my eyes opened to Open Source, the first area I pursued was the Data Layer. I started with NHibernate, but it turned out to be a bad fit. SubSonic was MUCH better, but it didn’t support Oracle very well. I did start making changes to the SubSonic source to get it to work for us, but then the corporate gods heard “Open Source” and decreed that SubSonic had to go. Nevermind that we had spent five months using it. I was given two weeks to come up with a new, homegrown, data layer. Well, two months later, we had our data layer. It reads the Oracle tables, views, functions, procs and packages and generates classes that can read/write/execute as appropriate. It turned out pretty well and is delightfully easy to use.

Next, up, the UI. The chosen toolset is Telerik RadControls for ASP.Net AJAX. (My previous experience is with Infragistics Windows toolset, so this was an interesting transition.) Users wanted the grid to be multi-edit. Teleriks grid does not support this natively, but it can be configured and coded to do it. But that is for another post.

Now, we are investigating MVC and Silverlight. I am hoping that my posts here will be of benefit to some as I explore these new technologies.