T4模板错误:加载包含文件ef.utility.cs.ttinclude返回空或空字符串 [英] T4 templates error: loading the include file ef.utility.cs.ttinclude returned a null or empty string

查看:3122
本文介绍了T4模板错误:加载包含文件ef.utility.cs.ttinclude返回空或空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经覆盖了控制器一代T4模板( ControllerWithContext.tt )所描述的<一个href=\"http://blogs.msdn.com/b/webdevtools/archive/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers.aspx\">here.

我想作为POCO模型生成T4模板以前需要在 EF.utility.CS.ttinclude 找到了code帮助工具的优势。所以我复制以下行从我的 Model.tt 我的 ControllerWithContext.tt

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

加载包含文件'EF.utility.CS.ttinclude'返回空或空字符串

Loading the include file 'EF.utility.CS.ttinclude' returned a null or empty string

按照 MSDN文档,这个错误是因为包含的文件是空白的,这这显然​​不是因为它的工作原理 Model.tt

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

我可以看到的唯一区别是,重写 ControllerWithContext.tt 不具有自定义工具定义的,而 Model.tt 有它设置为 TextTemplatingFileGenerator

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.

我的解决方法是我从 ef.utility.cs.ttinclude 所需要的功能复制到我的 ControllerWithContext.tt ,这本身就扔了更多的错误,但很容易解决了。

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.

我怎么能不包括自定义工具T4模板定义?

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

推荐答案

随着@DustinDavis的意见,并在使用的 OlegSych的网站,这里就是我所做的:

Following @DustinDavis' advice, and using the invaluable information found on OlegSych's site, here's what I did:


  • 创建了一个名为codeGenerationTools新项目。

  • 添加项目引用

  • Created a new project called CodeGenerationTools.
  • Added project references to


  • System.Data.Entity.Design

  • EnvDTE

  • System.Data.Entity的

  • Microsoft.VisualStudio.TextTemplating.10.0

有关这最后引用,我不得不安装Visual Studio SDK的正确版本

For this last reference I had to install the correct version of the Visual Studio SDK

扩展类:

public class CodeGenerationTools : TextTransformation


  • 重写 TransformText

    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;
    }
    


  • 最后,建这个项目。

  • 接下来的步骤发生在主体工程
    - 编辑的T4模板文件。
    - 改变模板指令

    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" #>
    

    所有这一切都意味着现在我可以用在我自己的T4模板EF.Utility.CS.ttinclude中发现的辅助方法,我不得不添加自己的辅助方法,这将提供给所有项目的手段。

    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.

    这篇关于T4模板错误:加载包含文件ef.utility.cs.ttinclude返回空或空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆