62,046
社区成员
发帖
与我相关
我的任务
分享
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.
<#@ 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));
}
#>
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();
}
}
#>