Home > Entity Framework > CodeFluent Entities vs Entity Framework: Naming convention

CodeFluent Entities vs Entity Framework: Naming convention


This is the first of many in a series of post covering the difference between Entity Framework 6 and CodeFluent Entities. Today we’re talking about naming convention.

Naming convention allows to transform the name of a concept in the model (entity, property, constraints, etc.) for generating database. For instance if you have an entity “Product” in your model, the generated table name may be “Product”, or “PRODUCT”, “prod” or maybe something else according to you naming convention. So naming convention is about standardization and also about making your DBA happy with the generating codeJ.

Entity Framework

Entity Framework Model-First doesn’t provide a simple way to apply a naming convention for the model. Instead you have to manually define names:

To automate the process you have to change default T4 templates located in Visual Studio installation folder, and the default generation workflow (TablePerTypeStrategy.xaml). Be sure to keep in sync the SQL template and the mapping (MSL) template.

Figure 1- Extract from the default T4 file

Then you can change model settings:

Entity Framework Code First has the notion of convention (does not work with Model First). This notion allows to set name of tables or properties:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Types().Configure(type => type.ToTable("EF_" + type.ClrType.Name));
     modelBuilder.Properties().Configure(prop => prop.HasColumnName("EF_" + prop.ClrPropertyInfo.Name));
}

Setting constraint names (foreign key names, index names) looks more complicated and requires more than one line of code.

CodeFluent Entities

CodeFluent Entities provides out of the box six naming conventions, and allows you to create your own naming convention.


 

To implement a custom naming convention you have to create a class that implements INamingConvention or inherits from BaseNamingConvention:

public class MyCustomConvention: BaseNamingConvention
{
 public override string GetName(INamedObject obj, IDictionary context)
 {
  Column column = obj as Column;
  if (column != null)
  {
   string name = (string)context["name"];
   Table table = (Table)context["table"];
   if (table != null)
   {
     column.DefaultName = "Default_" + table.Name + "_" + name;
   }
  }
  return base.GetName(obj, context);
 }

 public override string GetShortName(IShortNamedObject obj, IDictionary context)
 {
  Constraint constraint = obj as Constraint;
  if (constraint == null)
   return base.GetShortName(obj, context);

  return "myx_" + constraint.ConstraintType + "_" + constraint.Table.Name + "_" + constraint.ReferencedTable.Name;
 }
}

Conclusion

Entity Framework Model-First does not provide a simple way to define a naming convention. The Code-First approach is better for table name and column name, but it’s harder to name indices or foreign keys.

CodeFluent Entities provides out of the box six naming conventions, and let you write your own.

References :

 

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s