2016-09-30 52 views
0

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

下面是一个例子:

<#@ 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。”的引用有关。程序集,因为它们被模板和外部库引用,也许是不同的版本,但我不知道我会如何解决这个问题。有任何想法吗?

回答

0

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

我确定了Microsoft.SqlServer。引用分别设置复制本地,然后我更新了T4模板的安装段使用复制到生成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集版本必须由项目引用哪些组件已经不匹配。