Entity Metadata
If the idea of EF Code First is intriguing but you still prefer to use database-first development, you are not alone. EF Code First is great when ramping up, but the reality is that many development scenarios require that you work from a pre-baked database.
But what if you want to use MvcScaffolding? Things get a little tricky!
First of all, MvcScaffolding expects that you are using a database context similar to the one generated by EF Code First (instead of the ObjectContext created by default). This means that you have to download and install the full Entity Framework package and not just the EF package as a NuGet download. You have to manually add the DbContext code generation item to your EDMX file, and then you can start to use MvcScaffolding.
It’s only a couple of steps, but it pays off great gains…until you realize that your generated views have properties like SomePropWithNoSpacesInTitle or LocationID…in other words, they ain’t pretty. They also don't know anything about nullability or max length etc.
What MvcScaffolding.EntityMetadata Does
This is where this package steps in. EntityMetadata gives you the option to scaffold controllers by type with the appropriate metadata class generated for you. The metadata includes things like display name, required attributes and takes care of max length. It even sets up the partial class for your type and decorates it with your newly-created metadata class. Blamo!
Check out this post to see some before and after samples and instructions on how to install and use it.
Why Not Modify The DbContext Code Gen Tool To Support Metadata?
To be honest that’s where I started, and EntityMetadata is a heavily modified version of that tool, combined with some tricks from Scott Sanderson (MvcScaffolding examples) and some insight from Dan Wahlin (found his bits that dealt with max length/facets here).
But if you just modify DbContext’s templates you leave yourself in a bind: the generated classes would be continuously overwritten and you’d lose your changes. If you have to customize any of the generated classes (like modifying the data annotations) you’d lose your work every time the EDMX decides to run code gen.
So I went to the next closest thing: MvcScaffolding. After all, if you’re going to the trouble to add DbContext to your EDMX, it’s likely safe to assume you’re using MvcScaffolding, right?
Now, with a combination of T4 and PowerShell, this extension to MvcScaffolding does a one-time code generation for you, placing the new metadata classes into a Metadata folder in Models. You are free to modify it at will. EntityMetadata puts the process of creating your metadata classes inline with the generation of your controller and views. There are no additional steps.
Shoot! I Already Scaffolded My Views!
Shed no tears, fearless tryer of new things. The MvcScaffolding templates use Html.LabelFor() to display the name. You can just "scaffold" your metadata class without regenerating the controller and views, and the results are the same.
Read this post for all the details, and please leave any questions/comments/requests here on this page (I’ll set something up on CodePlex shortly).