Search code examples
t4camelcasing

T4 FieldName in camelCase without Underscore?


I'm using T4 to generate some class definitions and find that I'm getting an underscore in front of my field names.

I have set

code.CamelCaseFields = true;

just to be safe (even though I understand that's the default) but still end up with _myField rather than myField.

How can I generate a field name without the '_' character?

Also, where is the documentation for T4? I'm finding plenty of resources such as

Code Generation and Text Templates and numerous blogs, but I have not found the class-by-class, property-by-property documentation.


Solution

  • You're probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class is included via the <#@ include file="EF.Utility.CS.ttinclude"#> directive, which you can find at "[VSInstallDir]\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude".

    The FieldName function is defined as such:

    private string FieldName(string name)
    {
      if (CamelCaseFields)
      {
        return "_" + CamelCase(name);
      }
      else
      {
        return "_" + name;
      }
    }
    

    The "_" is hardcoded in the function. Coding your own shouldn't be difficult. Note that the CodeGenerationTools class is specific to this ttinclude file and isn't a generic and embedded way to generate code in T4.