Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

c# - T4 templates error: loading the include file ef.utility.cs.ttinclude returned a null or empty string

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 in EF.utility.CS.ttinclude as used in the POCO model generator T4 template. Therefore I copied the following lines from my Model.tt to my ControllerWithContext.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 the Model.tt has it set to TextTemplatingFileGenerator.

My workaround is to copy the functions I need from ef.utility.cs.ttinclude into my ControllerWithContext.tt, which in itself threw up more errors but which were easily solved.

How can I include T4 templates without a custom tool defined?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>" #> to using <name>;
  • Deleted the opening and closing <#+ #>
  • Added the directive using Microsoft.VisualStudio.TextTemplating;
  • Extended the class:

    public class CodeGenerationTools : TextTransformation
    
  • Override the TransformText method

    public 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 2010ProjectsCodeGenerationToolsCodeGenerationToolsinDebugCodeGenerationTools.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...