【问题标题】:T4 Template failing with MissingMethodException on imported assemblyT4 模板在导入的程序集中出现 MissingMethodException 失败
【发布时间】:2016-09-30 20:54:49
【问题描述】:

我创建了一个 T4 模板并决定创建一些帮助类来清理模板代码。我在我的帮助类的解决方案中创建了一个新的类项目,在我的模板中引用了程序集并导入了命名空间。

这是一个例子:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ assembly name="Microsoft.SqlServer.SmoExtended" #>
<#@ assembly name="$(SolutionDir)\MySolution.SqlMetaHelper\bin\Debug\MySolution.SqlMetaHelper    .dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="MySolution.SqlMetaHelper" #>
<#@ output extension=".txt" #>
<#
string @namespace = "MySolution.Data";
ServerConnection connection = new ServerConnection("localhost", "sa", "password");  
Server server = new Server(connection);
Database database = server.Databases["MySolution"];
#>
namespace <#= @namespace #>
{
<#
foreach (Table table in database.Tables)
{
#>
    public interface I<#= table.Name #>
    {
        //Properties
<#
    foreach (ColumnMeta column in table.Columns.Cast<Column>().Select(c => new ColumnMeta(c)))
    {
#>
        //<#=column.Name#>
<#
    }
#>
    }
<#
}
#>
}

模板执行失败,返回此错误:

Severity    Code    Description Project File    Line    Suppression State
Error       Running transformation: System.MissingMethodException: Method not found: 'Void MySolution.SqlMetaHelper.ColumnMeta..ctor(Microsoft.SqlServer.Management.Smo.Column)'.
   at Microsoft.VisualStudio.TextTemplatingE69FE551E9A42AE5D542A4EC2CDCECEDBDBC96F903EFDB8864E380652948850C270A5AC8C04E0B0F9368C0530BF3447DEAFC3716CC5CE03ABD37589675749A74.GeneratedTextTransformation.<>c.<TransformText>b__0_0(Column c)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.VisualStudio.TextTemplatingE69FE551E9A42AE5D542A4EC2CDCECEDBDBC96F903EFDB8864E380652948850C270A5AC8C04E0B0F9368C0530BF3447DEAFC3716CC5CE03ABD37589675749A74.GeneratedTextTransformation.TransformText() MySolution.Data C:\Users\me\documents\visual studio 2015\Projects\MySolution\MySolution.Data\Entities.tt    1

我尝试了很多事情都没有成功,我在网上找不到任何符合我的情况的东西。我觉得这与对“Microsoft.SqlServer.???”的引用有关程序集,因为它们被模板和外部库引用,并且可能是不同的版本,但我不确定如何解决这个问题。有什么想法吗?

【问题讨论】:

    标签: c# .net visual-studio templates t4


    【解决方案1】:

    我能够通过在帮助类项目中引用相同的程序集来解决此问题。

    我确定了 Microsoft.SqlServer.???引用设置为复制本地,然后我更新了 t4 模板的程序集部分以使用复制到构建文件夹的程序集,如下所示:

    <#@ assembly name="$(SolutionDir)\MySolution.SqlMetaHelper\bin\Debug\Microsoft.SqlServer.ConnectionInfo.dll" #>
    <#@ assembly name="$(SolutionDir)\MySolution.SqlMetaHelper\bin\Debug\Microsoft.SqlServer.Smo.dll" #>
    <#@ assembly name="$(SolutionDir)\MySolution.SqlMetaHelper\bin\Debug\MySolution.SqlMetaHelper.dll" #>
    

    GAC 程序集版本必须与项目引用的程序集不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      相关资源
      最近更新 更多