[I'm updating the title to reflect the answer I found, but leaving the question text unaltered]
This is basically the same question as How to output namespace in T4 templates?, but all of the answers there rely on accessing the Visual Studio host. As Brad Wilson comments in GarethJ's answer, these answers do not work in the MSBuild host via the Visualization and Modeling SDK.
My end goal is to add pregenerated views to an edmx file. It seems that VS has a hardcoded internal association from foo.edmx to foo.tt and foo.Context.tt. Changes to foo.edmx do not trigger my foo.Views.tt to run its transforms, even though it is <DependentUpon> foo.edmx in the project file.
Using the V&M SDK causes dependent text transforms to run when the parent changes, but all the namespaces get stripped out of the entity class files. EF.Utility.CS.ttinclude relies on the VS host, returning a null namespace if namespaceHint is not found.
Since I also don't want a dependency on $(DevEnvDir) in my MSBuild project, I have copied EF.Utility.CS.ttinclude to a source-controlled folder and am altering it to suit the MSBuild transform host.
I want to retrieve the same string returned by CallContext.LogicalGetData("NamespaceHint")
or by Host.ResolveParameterValue("", "", "namespaceHint")
. My current plan involves: passing $(RootNamespace) and $(ProjectDir) via <T4ParameterValues> and retrieving them in t4 with ResolveParameterValue, then getting the .tt file's directory relative to ProjectDir and converting slashes to periods, then concatenating onto RootNamespace. Ugly but in theory it will work.
I'm looking for one of the following:
- A method to retrieve the same value as Visual Studio's NamespaceHint through the V&M SDK, more simply than the concatenation described above.
- A method for setting up the proper dependency between foo.edmx and foo.arbitrary.tt without using the V&M SDK
- A method for excluding foo.tt and foo.Context.tt from being processed by the V&M SDK while still being processed by VS's built-in entity framework tools.
Either of latter would be slightly preferable to the former, but so far my research indicates that they are not possible. My current approach runs text transformation twice: VS does hardcoded edmx processing, then V&M SDK does the generic tt processing. If I get a really good answer for either of those, I will change the question title for posterity.
This question is also similar to: Getting the namespace for an edmx in a T4 template