简体   繁体   English

T4代:VsNamespaceSuggestion()从何处提取?

[英]T4 Generation: Where does VsNamespaceSuggestion() pull from?

Does anybody know, in a .tt file, where code.VsNamespaceSuggestion() gets its namespace from? 有谁知道在.tt文件中,code.VsNamespaceSuggestion()从何处获取其名称空间?

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. 但是,当.tt文件运行时,它总是将旧的名称空间放回其生成的类中。 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). 有人告诉我它来自EF模型,但是在那里看不到任何显示名称空间的名称(在设计器/属性中,或者通过在NotePad中打开.edmx文件)。

Any thoughts? 有什么想法吗? For a hack, I'm simply overriding it in the next line with the namespace I want: 对于hack,我只是在下一行中用所需的名称空间覆盖它:

string namespaceName = code.VsNamespaceSuggestion();
namespaceName = "Desired.Namespace"; //HACK:

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. 单击生成您的实体的代码生成* .tt文件。 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. 我的是空白的,所以T4从默认项目名称空间+文件夹层次结构生成名称空间。

The T4 host in Visual Studio pulls this from the CallContext property bag, using the name "NamespaceHint". Visual Studio中的T4主机使用名称“ NamespaceHint”将其从CallContext属性包中拉出。

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) 该值由标准T4定制工具(SingleFileGenerator)机制使用为定制工具指定的默认名称空间填充,该名称空间是从项目默认名称空间以及从项目根目录到tt文件的路径派生的,或者是在自定义工具规范下方的属性网格(如@mattkab在回答中所说)

If you're using the T4 service from code, you can populate this by setting up CallContext yourself. 如果您通过代码使用T4服务,则可以通过自己设置Call​​Context来填充。

Just modify the properties of *.tt files as following image, and effective immediately.: 只需将* .tt文件的属性修改为下图,即可立即生效:

默认为空

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. 为了确保安全,我在sra的评论后重新检查了所有项目的默认命名空间,但是一切确实是正确的。

"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 我使用了一个名为Agent Ransack的免费工具,它发现了一个名为.csproj的文件,其标签名为 , 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). AR还发现了一堆bin / Debug文件(abosluteFileList和缓存文件)中的旧名称空间。 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. AR还在我的.suo文件中找到了实例。 Since the problem seems to be fixed, I didn't delete that file (you can't edit it in NotePad, unfortunately). 由于问题似乎已解决,所以我没有删除该文件(很遗憾,您无法在NotePad中对其进行编辑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM