2015-07-10 20 views
0

我有这个错误>>>编译转换:未定义类型'DataTable'。DataTable出错

这是我组装的

<#@ template debug="true" hostspecific="true" language="VB" #> 
<#@ import namespace="System.Diagnostics" #> 
<#@ assembly name="System.Data" #> 
<#@ import namespace="System.Data.SqlClient" #> 
<#@ import namespace="System.Text" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ output extension=".vb" #> 

...何地发生

Imports project.Entities 
<# 
    Dim connectionString As String = "Data Source=PC101\TEST;Initial Catalog=MyDatabase;Integrated Security=True;" 
    Dim objTabla As String 
    Dim conjuntoDatos As String = objTabla & "Set" 
    Dim objContext as String = "context" & objTabla 

    Using connection As New SqlConnection(connectionString) 
     connection.Open() 

     System.Diagnostics.Debugger.Break 
     Dim mtable As DataTable = connection.GetSchema("Tables") 

     For liRows As Integer = 0 To mtable.Rows.Count - 1 
#> 
Public Sub New() 
    context = New <#=objContext #> 
    context.Configuration.ProxyCreationEnabled = False 
End Sub 
+0

我试着用这个代码制作一个t4模板 – ger

回答

0

所以代码片段你有一些错误的代码的一部分,使用的语句是失踪使用结束时,for缺少下一个,并且在初始化之前使用“objTabla”变量。在我解决了所有这些问题之后,我发现需要添加对System.Xml程序集的引用。下面是应该工作的完整模板:

<#@ template language="VB" hostspecific="true" debug="False" #> 
<#@ assembly name="System.Data" #> 
<#@ assembly name="System.Xml" #> 
<#@ import namespace="System" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.Linq" #> 
<#@ import namespace="System.Data.SqlClient" #> 
<#@ import namespace="System.Text" #> 
Imports project.Entities 
<# 
    Dim connectionString As String = "Data Source=PC101\TEST;Initial Catalog=MyDatabase;Integrated Security=True;" 
    Dim objTabla As String = "" 
    Dim conjuntoDatos As String = objTabla & "Set" 
    Dim objContext as String = "context" & objTabla 

    Using connection As New SqlConnection(connectionString) 
     connection.Open() 

     System.Diagnostics.Debugger.Break 
     Dim mtable As DataTable = connection.GetSchema("Tables") 

     For liRows As Integer = 0 To mtable.Rows.Count - 1 

#> 
Public Sub New() 
    context = New <#=objContext #> 
    context.Configuration.ProxyCreationEnabled = False 
End Sub 
<# 
    next 

    end using 
#> 

如果你有兴趣在如何我想通了这一点,我用我的分机T4 Awesome调试和测试您的模板。完全免责声明,我使用的功能是完整版本,这需要花费(但你可以免费试用14天)。你可以使用免费的社区版本,它会告诉你同样的事情,但以一种不太友好的方式,你必须在vs输出窗口中查看原始日志以获得答案。