【发布时间】: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