25

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:
sra
  • 23,820
  • 7
  • 55
  • 89
James in Indy
  • 2,804
  • 2
  • 25
  • 25

4 Answers4

58

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.

mattkab
  • 728
  • 5
  • 8
3

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.

GarethJ
  • 6,496
  • 32
  • 42
3

Just modify the properties of *.tt files as following image, and effective immediately.:

Default is empty

Community
  • 1
  • 1
tomexou
  • 343
  • 2
  • 5
1

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).

James in Indy
  • 2,804
  • 2
  • 25
  • 25