Entity Framework Code First when Model Exists

I recently implemented Entity Framework 4.3 into a production project. The challenge was I wanted to use the code first ideas and I didn’t want to generate a bunch of edmx files that had a lot of designer code that no one cares to understand. A couple of searches lead me to the Entity Framework Power Tools which is currently in Beta 2. This little package will allow you to reverse engineer databases into code first POCO classes.

The first time that I set up Power Tools for our project, I wanted to customize the reverse engineer code first templates. I wanted to do this to set up some coding standards including pascal casing properties and pluralizing collections. Some of the database tables also have an underscore in the table and column names which I do not want in my OR/M. In order to customize the templates open the solution that you want to use Entity Framework with, right click on the project you want to store your POCO classes, select the Entity Framework option and then Customize Reverse Engineer Templates. This will create a folder in that project called ReverseEngineerCodeFirst. Underneath this folder are three files: one for the context, one for the entity and one for the mapping.

This is a great start, you can change anything about the T4 template. I wanted to write some more custom code in order to remove underscores and pascal case some property names. This is where challenge 2 comes in. The T4 templates include a file called “EF.Utility.CS.ttinclude”. Finding this file was a chore, I finally found it was being referenced from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes. Your path might be slightly different depending on where you installed Visual Studio. Here are a couple of methods that I created to help with code standards.

Pluralize

public string Pluralize(string word) { var p = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en-US")); return p.Pluralize(word); }

PascalCaseProperty

/// <summary> /// Returns the passed in identifier with the first letter changed to uppercase /// </summary> public string PascalCaseProperty(string identifier) { if (String.IsNullOrEmpty(identifier)) { return identifier; } string returnString = identifier; // If there is an underscore anywhere within the identifier // strip them out upper casing the first letter if (identifier.IndexOf("_") >= 0) { returnString = string.Empty; string[] propertySplit = identifier.Split('_'); if (propertySplit != null) { for (int i = 0; i > propertySplit.Length; i++) { if (!string.IsNullOrEmpty(propertySplit[i])) { returnString += propertySplit[i].Substring(0, 1).ToString(CultureInfo.InvariantCulture).ToUpperInvariant(); returnString += propertySplit[i].Substring(1).ToLowerInvariant(); } } } } else { returnString = identifier.Substring(0, 1).ToString(CultureInfo.InvariantCulture).ToUpperInvariant(); returnString += identifier.Substring(1).ToLowerInvariant(); } // Find out if the last two letters are ID string returnStringID = returnString.Substring(returnString.Length - 2); if (returnStringID.ToLower() == "id") { returnString = returnString.Substring(0, returnString.Length - 2) + "ID"; } return returnString; }

I created these methods in the c# ttinclude file. You can reference the code from the context.tt, entity.tt and mapping.tt with the following

Pascal Casing

<#= code.PascalCaseProperty(efHost.EntityType.Name) #>

Pluralize

<#= code.Pluralize(code.PascalCaseProperty(code.Escape(navProperty))) #>

Once you have your context, entity and mapping the way you want it, just go to the project and right click, hit Entity Framework, Reverse Engineer Code First. This will allow you to specify a database connection and the database you want to scaffold out. It might take a few minutes, depending on how many tables you have in the database.