I'm investigating replacing or supplementing our home grown ORM system with the Entity Framework 4, and I'm noticing that the latter may end up causing a conflict between what we've defined as the naming conventions for our programming code and our databases. Being a Microsoft shop, we've largely decided to follow Microsoft's naming guidelines for our code, which say to use Pascal casing for members, namespaces, etc.; to avoid using underscores, and so on.
The default entity naming conventions in EF4, not surprisingly, work great with these standards. For example, an entity named SalesOrder will generate a class named SalesOrder and an entity set named SalesOrders. An EF4 Model-First design will, by default, generate a table of the same name as the entity set (in this example, the generated table name is SalesOrders). However, our database standards suggest to use all lowercase and underscores between words (e.g., sales_orders). So, using the Entity Framework "as is" will cause us to start deviating from them.
Is there anywhere in the Entity Framework where you can override it's behavior to use the entity set name as the SQL table name? I can't seem to find an obvious place to specify an alternate table name for the generated SQL script. If we go forward with using EF4, is the only plausible solution to have us reconsider our database naming conventions?
Update:
I'm trying Ladislav's solution below, but I can't seem to get the Generate Database from Model option in the Entity Framework model designer to recognize my custom utility. I have a file named MyOrg.EF.Utility.CS.ttinclude in the folder:
%VSINSTALLDIR%\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes
It essentially looks like this:
<#@ import namespace="Microsoft.CSharp"#>
<#@ import namespace="System.Text"#>
public class CustomUtilities
{
public static string EntityNameToSqlName(string name)
{
string sqlName = ""; // The table name based on the input model name
string pattern = "([A-Z]+[s])|([A-Z](?=[a-z]))|((?<=[a-z])[A-Z])"; //Pattern for the regex exp. below
// Separate out each word with spaces:
sqlName = System.Text.RegularExpressions.Regex.Replace(name, pattern, " $&");
// Replace spaces with underscores and then make lowercase:
sqlName = sqlName.Trim().Replace(" ", "_").ToLower();
return sqlName;
}
}
I've tried to reference this file in my custom .tt DDL generation file near the top as such:
<#@ include file="MyOrg.EF.Utility.CS.ttinclude"#>
However, if I try to reference the above function using code like this in the .tt file:
string tableName = CustomUtilities.EntityNameToSqlName(Id(entitySet.GetTableName()));
Visual Studio then complains that The name 'CustomUtilities' does not exist in the current context. Removing the class name from "CustomUtilities.EntityNameToSqlName" returns a similar error. Should I try a different way to insert a custom function into the DDL generation code?
Final solution:
I was able to finally get this working after I realized that I didn't wrap the C# code in my MyOrg.EF.Utility.CS.ttinclude file with this:
<#+
[my code]
#>
I also needed to add a public copy of the WriteColumns() method found in the file GenerateTSQL.Utility so that it would use my EntityNametoSqlName() method.
Unfortunately, my customized version of the original SSDLToSQL10.tt file is now a little messy, since I need to wrap CustomUtilities.EntityNameToSqlName() around quite a few items in there.