T4 templates error: loading the include file ef.utility.cs.ttinclude returned a null or empty string
Solution 1
Following @DustinDavis' advice, and using the invaluable information found on OlegSych's site, here's what I did:
- Created a new project called CodeGenerationTools.
-
Added project references to
- System.Data.Entity.Design
- EnvDTE
- System.Data.Entity
- Microsoft.VisualStudio.TextTemplating.10.0
For this last reference I had to install the correct version of the Visual Studio SDK
- Copied the EF.Utility.CS.ttinclude file into the project.
- Renamed it CodeGenerationTools.cs
- Edited the file and convert all
<#@ import namespace="<name>" #>
tousing <name>;
- Deleted the opening and closing
<#+ #>
- Added the directive
using Microsoft.VisualStudio.TextTemplating;
-
Extended the class:
public class CodeGenerationTools : TextTransformation
-
Override the
TransformText
methodpublic override string TransformText() { throw new NotImplementedException(); }
-
Added empty constructor
public CodeGenerationTools() { _textTransformation = DynamicTextTransformation.Create(this); _code = new CSharpCodeProvider(); _ef = new MetadataTools(_textTransformation); FullyQualifySystemTypes = false; CamelCaseFields = true; }
- Finally, build this project.
The next steps took place in the main project - Edited the T4 template file. - Changed template directive to
<#@ template language="C#" HostSpecific="True" debug="false" inherits="CodeGenerationTools"#>
- Added the directives
<#@ assembly name="C:\Visual Studio 2010\Projects\CodeGenerationTools\CodeGenerationTools\bin\Debug\CodeGenerationTools.dll" #>
<#@ import namespace="CodeGenerationTools" #>
All of which now means I can use the helper methods found in EF.Utility.CS.ttinclude in my own T4 templates, and I have the means to add my own helper methods which will be available to all projects.
Solution 2
If you have Visual Studio 2012 or 2013, install this EF tool to resolve the error.
Solution 3
The answer is that the template processor isn't even trying to get the include file (as confirmed using ProcMon). You can reproduce this using any template, not just the EF.Utility.CS.ttinlcude
Not sure why you need the code but you can always build your own base class, just have it inherit from Microsoft.VisualStudio.TextTemplating.TextTransformation and then put in all the code thats is in the EF.Utility file. Then set the inherits directive to point to your new base class and then you can access those methods from your template.
Related videos on Youtube
Mark_Gibson
Started with C; added HTML and Javascript; added a pinch of PERL; migrated to ColdFusion; enhanced with SQL; added PHP with MySQL; added some VB.NET for good measure; some more Coldfusion; now immersed in ASP.NET and C#. If a web log becomes a blog, then surely a web app becomes a bapp. Therefore I am a bapp developer. You heard it here first! Increase my population: http://webbie4.myminicity.com/
Updated on June 04, 2022Comments
-
Mark_Gibson almost 2 years
I have overridden the controller generation T4 templates (
ControllerWithContext.tt
) as described here.
I would like to take advantage of the code helper utilities found inEF.utility.CS.ttinclude
as used in the POCO model generator T4 template. Therefore I copied the following lines from myModel.tt
to myControllerWithContext.tt
.<#@ include file="EF.Utility.CS.ttinclude"#>
However, when I try to add a controller I am getting the error message
Loading the include file 'EF.utility.CS.ttinclude' returned a null or empty string
According to the MSDN documentation, this error is because the included file is blank, which it clearly isn't because it works with
Model.tt
The only difference I can see is that the overridden
ControllerWithContext.tt
does not have a Custom Tool defined, whereas theModel.tt
has it set toTextTemplatingFileGenerator
.My workaround is to copy the functions I need from
ef.utility.cs.ttinclude
into myControllerWithContext.tt
, which in itself threw up more errors but which were easily solved.How can I include T4 templates without a custom tool defined?
-
Mark_Gibson over 12 yearsThanks. As I mentioned I wanted to take advantage of the
CodeGenerationTools
class found inef.utility.cs.ttinclude
in my own T4 templates. -
Dustin Davis over 12 yearsJust follow what i suggested and you can, just not by using an include (at least for now because it seems to be broken)
-
Joshua Frank over 10 years@DustinDavis: are you saying that
<#@ include file="..." #>
simply doesn't work? -
Shady Mohamed Sherif about 8 yearsYour answer saved me. really thanks for help. You have to be answer #1
-
Reza over 6 yearsstill issue for me