Referring EDMX file in Separate VS Project from T4 Template
- by Paul Petrov
In my project I needed to separate template generated entities, context in separate projects from the EDMX file. I’ve stumbled across this problem how to make template generator to find edmx file without hardcoding absolute path into the template. Using relative path directly (inputFile=@”..\ProjectFolder\DataModel.edmx”) generated error:
Error 2 Running transformation: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\ProjectFolder\DataModel.edmx'
The code that worked well for me when placed in the beginning of the .tt file:
…
string rootPath = Host.ResolvePath(String.Empty);
string relativePath = @"..\\ProjectDir\\DataModel.edmx";
string inputFile = Path.Combine(rootPath, relativePath);
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
…