T4 Field name in camelCase without underscore?

I use T4 to generate class definitions and find that I get an underscore in front of field names.

I have installed

code.CamelCaseFields = true; 

just to be safe (although I understand that by default), but still end up with _myField, not myField.

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

Also, where is the documentation for the T4? I find many resources like

Code generation and text templates and numerous blogs, but I did not find the documentation for each property individually.

+3
source share
3 answers

You are probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class CodeGenerationTools included in the <#@ include file="EF.Utility.CS.ttinclude"#> directive, which you can find in the "[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; } } 

"_" is hard-coded into the function. Coding your own should not be difficult. Note that the CodeGenerationTools class CodeGenerationTools specific to this ttinclude file and is not a general and built-in way to generate code in T4.

+3
source

I wrote the following method to make the first case uppercase, remove spaces / underscores and make the next uppercase character. See examples below. Feel free to use.

 private string CodeName(string name) { name = name.ToLowerInvariant(); string result = name; bool upperCase = false; result = string.Empty; for (int i = 0; i < name.Length; i++) { if (name[i] == ' ' || name[i] == '_') { upperCase = true; } else { if (i == 0 || upperCase) { result += name[i].ToString().ToUpperInvariant(); upperCase = false; } else { result += name[i]; } } } return result; } 

I / O samples: first_name = FirstName, id = Id, status message = StatusMessage

+2
source

This is good advice, but it will not help you find out exactly where the right place for such a function ...

Is there any guidance on DECOMPOSING EF.tt files or step-by-step through the output view to see how it creates the output?

I was able to successfully use this function by connecting it to a function called (Ef4.3)

public string Property (EdmProperty edmProperty)

It seems to be used to output strings like "public int fieldname {get; set;}"

and changed parameter 3 (index {2}) to form in order to wrap the function for changing the name, for example:

 _typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name" _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged 

This is not ideal, for example: it does not preserve the existing "Ucasing" and does not care about such things: customerIP outputs: Customerip that IMO is not very readable ...

but it’s better than I looked at what was a nightmare because the database was mixed with camelCase, PascalCase mess and underline separation, so it’s pretty terrible.

In any case, this will help someone ...

0
source

Source: https://habr.com/ru/post/1209270/


All Articles