T4 FieldName 采用驼峰命名法,不带下划线?
我正在使用 T4 生成一些类定义,并发现我的字段名称前面有一个下划线。
我设置
code.CamelCaseFields = true;
只是为了安全(尽管我知道这是默认设置),但最终仍然是 _myField 而不是 myField。
如何生成不带“_”字符的字段名称?
另外,T4的文档在哪里?我找到了大量资源,例如
代码生成和文本模板以及大量博客,但我还没有找到逐个类、逐个属性的文档。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能正在谈论 EF4 自跟踪实体。
CodeGenerationTools
类通过<#@ include file="EF.Utility.CS.ttinclude"#>
指令包含,您可以在“[VSInstallDir”中找到该指令] \ Common7 \ IDE \ Extensions \ Microsoft \ Entity Framework Tools \ Templates \ Includes \ EF.Utility.CS.ttinclude”。FieldName 函数的定义如下:
“_”在函数中被硬编码。自己编写代码应该不难。请注意,
CodeGenerationTools
类特定于此 ttinclude 文件,并不是在 T4 中生成代码的通用嵌入式方式。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:
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.我编写了以下方法来使第一个字符大写,删除空格/下划线并使下一个字符大写。请参阅下面的示例。请随意使用。
输入/输出样本:
名字 = 名字,
id = id,
状态消息 = StatusMessage
I've written the following method to make first character upper case, remove spaces/underscores and make next character upper case. See samples below. Feel free to use.
input/output samples:
first_name = FirstName,
id = Id,
status message = StatusMessage
这是一个很好的建议,但它并不能帮助您了解放置此类函数的正确位置在哪里...
是否有关于分解 EF .tt 文件或逐步生成输出以查看它如何构建输出的指导?
通过将上面的函数插入一个名为的函数中,我能够成功地使用它
(Ef4.3)
public string Property(EdmProperty edmProperty)
似乎用于输出诸如“public int fieldname { get; set; }”之类的行,
并将第三个(索引 {2})参数更改为要包装的格式修改名称的函数,如下所示:
这并不完美,例如:它不会保留现有的“Ucasing”并且不关心这样的事情:
客户IP
输出:客户
在我看来,它的可读性不是很好……
但它比我所看到的要好,这是一场噩梦,因为数据库混杂着驼峰命名法、帕斯卡命名法和下划线分隔符,非常可怕。
无论如何希望这对某人有帮助......
This is good advice however it doesn't help you in knowing WHERE the right place to put such a function is...
Is there any guidance on DECOMPOSING the EF .tt files or stepping through the output generation to see how it builds the output?
I was able to use the above function successfully by plugging it into a function called
(Ef4.3)
public string Property(EdmProperty edmProperty)
Which appears to be used to output the lines like "public int fieldname { get; set; }"
and changed the 3rd (index {2}) param to the formating to wrap with the function to modify the name, like this:
This is not perfect, eg: it doesn't keep existing "Ucasing" and doesn't care about things like this:
customerIP
outputs: Customerip
which IMO is not very readable...
but its better than what I WAS looking at which was a nightmare because the database was intermingled mess of camelCase, PascalCase and underscore separation, so pretty horrific.
anyway hope this helps someone...