Custom Entity Naming Rule for EF

I have an edmx file in my Visual Studio project, when you insert a table from your model into edmx, it gets the same name as in the database, for example, importing myTable , you get an object called myTable with Set Name myTables .

How can I make tables stick to user-defined naming conventions when adding them to my file? So, for example, adding myTable results in an object named tblmyTable with Set Name tblmyTables .

+7
visual-studio entity-framework
source share
1 answer

If you are using the old Entity Object EF4.0 or EF5.0:

You need to modify the <YourModelName>.tt in several places.

The template calls container.BaseEntitySets.OfType<EntitySet>() to get all your entities, where the returned EntityeSet.ElementType (where the ElementType type is EntityType ) contains information about your entities.

These objects are used in many places in the template, so the easiest solution would be to set the Name property to EntityType . However, the Name property is internal, so you need to use refection to set it.

So, find the following line in the pattern around line 163 :

 region.Begin(CodeGenerationTools.GetResourceString("Template_RegionObjectSetProperties")); foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>()) { 

And you need to add the following code here to set the Name property:

 region.Begin(CodeGenerationTools.GetResourceString("Template_RegionObjectSetProperties")); foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>()) { set.ElementType.GetType() .GetProperty("Name", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty) .SetValue(set.ElementType, "tbl" + set.ElementType.Name, null); 

Unfortunately, you are not done yet, because you still have to change two places:

At first, the generated property names are not fixed, so you need to find the following line around line 173 :

 <#=code.SpaceAfter(NewModifier(set))#><#=Accessibility.ForReadOnlyProperty(set)#> ObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>> <#=code.Escape(set)#> 

And change it to (add tbl to <#=code.Escape(set)#> :

 <#=code.SpaceAfter(NewModifier(set))#><#=Accessibility.ForReadOnlyProperty(set)#> ObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>> tbl<#=code.Escape(set)#> 

Because of reflection hacking, you need to remove the “tbl” prefix generated by EdmEntityTypeAttribute , so you need to find the following line around the line : line 295 :

 [EdmEntityTypeAttribute(NamespaceName="<#=entity.NamespaceName#>", Name="<#=entity.Name#>")] 

And change it to:

 [EdmEntityTypeAttribute(NamespaceName="<#=entity.NamespaceName#>", Name="<#=entity.Name.Replace("tbl","")#>")] 

If you are using the standard EF DbContext generator EF5.0 or EF6.0 with edmx:

You need to modify the tt files to apply your usual naming convention.

First you must modify the <YourEdmxName>.tt :

Around line 23 you should find the following method call:

 fileManager.StartNewFile(entity.Name + ".cs"); 

This method breaks up entity classes, so if you want to change the generated file names, you need to change them to:

 fileManager.StartNewFile("tbl" + entity.Name + ".cs"); 

Then around line 307 you need to find the following method declaration:

 public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); } 

These methods write class names, so here you need to change it to apply your convention:

 public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), "tbl" + _code.Escape(entity), // add tbl prefix before the entity name _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); } 

Finally, you need to change your <YourEdmxName>.Context.tt :

Around line 296, you need to find the following method, which writes out the properties of the DbSet :

 public string DbSet(EntitySet entitySet) { return string.Format( CultureInfo.InvariantCulture, "{0} DbSet<{1}> {2} {{ get; set; }}", Accessibility.ForReadOnlyProperty(entitySet), _typeMapper.GetTypeName(entitySet.ElementType), _code.Escape(entitySet)); } 

And change it again to apply your agreement:

 public string DbSet(EntitySet entitySet) { return string.Format( CultureInfo.InvariantCulture, "{0} DbSet<{1}> {2} {{ get; set; }}", Accessibility.ForReadOnlyProperty(entitySet), // add "tbl" to the type name DbSet<Table> -> DbSet<tblTable> "tbl" + _typeMapper.GetTypeName(entitySet.ElementType), // add "tbl" to property name Tables -> tblTables "tbl" + _code.Escape(entitySet)); } 
+6
source share

All Articles