簡體   English   中英

T4模板在導入的程序集上失敗,缺少MissingMethodException

[英]T4 Template failing with MissingMethodException on imported assembly

我已經創建了一個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。???”的引用有關。 程序集,因為它們同時被模板和外部庫引用,並且可能是不同的版本,但是我不確定如何解決。 有任何想法嗎?

我可以通過在助手類項目中引用相同的程序集來解決此問題。

我確保了Microsoft.SqlServer。 引用被設置為復制本地,然后我更新了t4模板的assembly部分,以使用復制到build foler的程序集,如下所示:

<#@ 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程序集版本必須與項目引用的程序集不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM