T4 一代:VsNamespaceSuggestion() 从哪里获取?
有谁知道,在 .tt 文件中, code.VsNamespaceSuggestion() 从哪里获取其命名空间?
我遇到一个问题,我必须更改解决方案的命名空间,并且我发誓我已经在所有地方更改了它(文件夹名称、文件名、项目名称、项目属性、程序集信息等)。但是当 .tt 文件运行时,它总是将旧的命名空间放回其生成的类中。有人告诉我它来自 EF 模型,但我在其中看不到任何显示名称空间的内容(无论是在设计器/属性中还是通过在记事本中打开 .edmx 文件)。
有什么想法吗?对于黑客来说,我只是在下一行中用我想要的命名空间覆盖它:
string namespaceName = code.VsNamespaceSuggestion();
namespaceName = "Desired.Namespace"; //HACK:
Does anybody know, in a .tt file, where code.VsNamespaceSuggestion() gets its namespace from?
I'm having an issue where I had to change a solution's namespace, and I swear I've changed it everywhere (folder names, filenames, project names, project properties, assembly info, etc). But when the .tt file runs, it always puts the old namespace back in its generated classes. I'm told it comes from the EF model, but I see nothing in there that shows a namespace (either in the designer/properties or by opening the .edmx file in NotePad).
Any thoughts? For a hack, I'm simply overriding it in the next line with the namespace I want:
string namespaceName = code.VsNamespaceSuggestion();
namespaceName = "Desired.Namespace"; //HACK:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
刚刚遇到了这个确切的问题,并且我发现的解决方案没有在这里提到,所以我想我可以帮助任何在搜索中遇到这篇文章的人。
单击正在生成实体的代码生成 *.tt 文件。在属性窗格中,将自定义工具命名空间设置为您想要生成的命名空间。我的是空白的,因此 T4 从默认项目命名空间 + 文件夹层次结构生成命名空间。
Just had this exact problem, and the solution I discovered wasn't mentioned here, so I thought I'd help anyone that came across this article in a search.
Click on the code-generation *.tt file that is generating your entities. In the properties pane, set the Custom Tool Namespace to the namespace you would like generated. Mine was blank, so T4 was generating the namespace from the default project namespace + folder heirarchy.
Visual Studio 中的 T4 主机使用名称“NamespaceHint”从 CallContext 属性包中提取此内容。
该值由标准 T4 自定义工具 (SingleFileGenerator) 机制使用为自定义工具指定的默认命名空间来填充,该命名空间要么派生自项目默认命名空间以及从项目根目录到 tt 文件的路径,要么在自定义工具规范下方的属性网格(如 @mattkab 在他的回答中所述)
如果您通过代码使用 T4 服务,则可以通过自己设置 CallContext 来填充它。
The T4 host in Visual Studio pulls this from the CallContext property bag, using the name "NamespaceHint".
That value is populated by the standard T4 custom tool (SingleFileGenerator) mechanism using the default namespace specified for the custom tool, which is either derived from the project default namespace and the path from the project root to the tt file or is explicitly specified in the property grid below the custom tool specification (as @mattkab says in his answer)
If you're using the T4 service from code, you can populate this by setting up CallContext yourself.
只需修改 *.tt 文件的属性如下图,立即生效:
Just modify the properties of *.tt files as following image, and effective immediately.:
我想我已经解决了这个问题。在 sra 发表评论后,我重新检查了所有项目的默认命名空间,以确保一切都正确。
“查找文件中的所有内容”没有找到任何内容。我使用了一个名为 Agent Ransack 的免费小工具,它发现了一个带有名为 标记的 .csproj 文件,其中包含旧的命名空间。我改变了它,这就是解决它的方法。
AR还在一堆bin/Debug文件(abosoluteFileList和缓存文件)中发现了旧的命名空间。我删除了所有这些文件,以便使用新的命名空间重新生成它们。我不知道这是否有任何影响。
AR 还在我的 .suo 文件中找到了实例。由于问题似乎已解决,因此我没有删除该文件(不幸的是,您无法在记事本中编辑它)。
I think I've fixed the problem. I re-checked all projects' Default Namespaces after sra's comment just to make sure, but everything was indeed correct.
"Find All In Files" didn't turn up anything. I used a little free tool called Agent Ransack, and it found a .csproj file with a tag named , which had the old namespace in it. I changed that, and THAT's what fixed it.
AR also found the old namespace in a bunch of bin/Debug files (abosluteFileList and cache files). I deleted all of those files so they would be regenerated with the new namespace. I have no idea if that had any effect.
AR also found instances in my .suo file. Since the problem seems to be fixed, I didn't delete that file (you can't edit it in NotePad, unfortunately).