Question

[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

Was it helpful?

Solution

This It seems that VS has a hardcoded internal association from foo.edmx to foo.tt and foo.Context.tt. is actually not true. The association is not hardcoded per-se - there is actually a very simple hack that allows triggering any T4 generation upon saving an edmx. Just put the name of the edmx file anywhere in the template. I elaborated a bit more about this here where I just showed that just putting

// Model1.edmx 

(notice it's just a comment) in the T4 template bound the T4 template to the edmx file and therefore the transformation started to happen each time the edmx file was being saved.

OTHER TIPS

I was making this way harder on myself than necessary.

This is the entirety of the changes I needed to make to my .csproj to add pregenerated views to my entity model at path\to\foo.edmx:

  <ItemGroup>
    <Compile Include="path\to\foo.Views.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>foo.Views.tt</DependentUpon>
    </Compile>
    <T4Transform Include="path\to\foo.Views.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>foo.Views.cs</LastGenOutput>
      <DependentUpon>foo.edmx</DependentUpon>
    </T4Transform>
  </ItemGroup>
  <Target Name="GenerateViews" BeforeTargets="EntityDeploy">
    <CallTarget Targets="ExecuteTransformations" />
  </Target>
  <Import Project="$(ExternalLibrariesFolder)\TextTemplateTransformationToolkit\Microsoft.TextTemplating.targets" />

foo.Views.tt is a copy of the EF4/EF5 Model/Database First View Gen .tt for C#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top