T4模板 利用T4Toolbox生成实体类的问题

liao_ken 2016-09-09 02:16:48
我使用T4 toolbox想实现T4根据数据库生成多个实体类,但提了点问题解释不了,请大神帮忙看看,错误信息:
A Statement cannot appear after the first class feature in the template. Only boilerplate, expressions and other class features are allowed after the first class feature block.


模板分为两个EntityScript.tt和EntityTemplate.ttinclude
EntityScript.tt代码如下

<#@ template language="C#" debug="False" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Data.DataSetExtensions" #>
<#@ assembly name="System.Xml" #>
<#@ Assembly Name="$(SolutionDir)\Mvc5.Core\bin\Debug\Mvc5.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Mvc5.Core.T4" #>
<#@ output extension=".cs" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="Include\EntityTemplate.ttinclude" #>

<#
System.Diagnostics.Debugger.Launch();
DatabaseInfo di = new DatabaseInfo();
DataTable dt = di.GetTableStructure();//获取数据表结构
string _namespace = di.GetType().Namespace.Replace("T4", "Entity");
EnumerableRowCollection<DataRow> dtRow = dt.AsEnumerable();
var tables = (from d in dtRow select new { TableName = d.Field<string>("Table_Name"), TableDescription = d.Field<string>("Table_Desc") }).Distinct().ToList();
List<T4EntityInfo> entities = (from t in tables
select new T4EntityInfo
{
NameSpace = _namespace,
TableName = t.TableName,
TableDescription = t.TableDescription
}).ToList();
//string currentPath = //Path.GetDirectoryName(Host.TemplateFile);
string projectPath = Host.ResolveAssemblyReference("$(ProjectDir)"); //currentPath.Substring(0, currentPath.IndexOf(@"\T4"));
string solutionPath = Host.ResolveAssemblyReference("$(SolutionDir)"); //currentPath.Substring(0, currentPath.IndexOf(@"\Mvc5.Core"));
System.Diagnostics.Debugger.Break();
foreach (T4EntityInfo entity in entities)
{
DataRow[] drs = dt.Select(string.Format("Table_Name='{0}'", entity.TableName));
entity.Columns = new List<T4ColumnInfo>();
foreach(DataRow dr in drs)
{
T4ColumnInfo col = new T4ColumnInfo();
col.ColIndex = int.Parse(dr["Column_Index"].ToString());
col.ColName = dr["Column_Name"].ToString();
col.ColDescription = dr["Column_Desc"].ToString();
col.ColIsKey = dr["Column_IsKey"].ToString() == "1" ? true : false;
col.ColIsIdentity= dr["Column_IsIdentity"].ToString() == "1" ? true : false;
col.ColDataType = di.TransformSqlType(dr["Column_DataType"].ToString(),(dr["Column_Is_Null"].ToString() == "1" ? true : false));
col.ColIsNull = dr["Column_Is_Null"].ToString() == "1" ? true : false;
col.ColMaxLength = int.Parse(dr["Column_Max_Length"].ToString());
col.ColMaxLengthByte = int.Parse(dr["Column_Max_Length_Byte"].ToString());
col.ColDecimalDigits = int.Parse(dr["Column_Decimal_Digits"].ToString());
col.ColDefaultValue = dr["Column_Default_Value"].ToString();
entity.Columns.Add(col);
}
var paths = string.Format(@"{0}\Entity", projectPath);
EntityTemplate template = new EntityTemplate(entity);
template.Output.Encoding = Encoding.UTF8;
template.RenderToFile(Path.Combine(paths, entity.TableName));

}
#>



EntityTemplate.ttinclude代码如下

public class EntityTemplate : CSharpTemplate
{
private T4EntityInfo _model;
private T4ColumnInfo _col;

public EntityTemplate(T4EntityInfo model){
_model = model;
}
public override string TransformText()
{
base.TransformText();
#>
//------------------------------------------------------------------
// 该代码由工模板工具生成,对此文件的更改可能会导致不正确的行为
// 生成时间:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") #>
//------------------------------------------------------------------
using System;
using System.ComponentModel.DataAnnotations;
namespace <#= _model.NameSpace #>
{
public partial class <#= _model.TableName #>
{
<#+foreach(var col in _model.Columns){#> //这里报错误不允许声明
public <#=col.ColDataType #> <#=col.ColName #> { get;set;}
<#}#>}
}
}
<#+
return this.GenerationEnvironment.ToString();
}
}
#>
...全文
372 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hualuorenjia 2016-09-27
  • 打赏
  • 举报
回复
<#+foreach(var col in _model.Columns){#> //这里报错误不允许声明 public <#=col.ColDataType #> <#=col.ColName #> { get;set;} <#}#>} 改为 <#+foreach(var col in _model.Columns){#> //这里报错误不允许声明 public <#=col.ColDataType #> <#=col.ColName #> { get;set;} <#+}#>}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧