CodeSmith笔记
CodeSmith 部分学习 1. 注册子模板child.cst到主模板 <%@ Register Name="ChildTemplate" Template="child.cst" MergeProperties="False" ExcludeProperties="" %> 2. 调用子模板并输出 <% for(int i = 0; i < ThisTableList.Count; i++) { CodeTemplate b = new ChildTemplate(); b.ThisTable = ThisTableList[i]; b.Render(this.Response); } %> 3. 遍历TableCollection表,并输出表名 <% for (int t = 0; t < ThisDatabase.Tables.Count; t++) { %> <%= ThisDatabase.Tables[t].Name %> <% } %> 4. 遍历Table列,其中DataType2CSharpType为一个自定义方法 <% for (int c = 0; c < ThisTable.Columns.Count; c++) { %> //数据库类型:DbType.<%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> private <%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> _<%= ThisIsTable.Columns[c].Name %>; <% } %> 5. 遍历Table的PK <% for(int i=0;i<ThisTable.PrimaryKey.MemberColumns.Count;i++) {%> //主键名<%= i%>:<%= ThisTable.PrimaryKey.Name%> <%= ThisTable.PrimaryKey.Table.Name %>.<%= ThisTable.PrimaryKey.MemberColumns[i].Name %> <%}%> 6. 遍历Table的FK(从表明细)(以1:1为例) <% for(int i=0;i<ThisTable.ForeignKeys.Count;i++){%> //外键<%= i %>:<%= ThisTable.ForeignKeys[i].Name %> //外键<%= i %>对应的列 <% for(int j=0;j<ThisTable.ForeignKeys[i].PrimaryKeyMemberColumns.Count;j++){%> <%= ThisTable.ForeignKeys[i].ForeignKeyTable.Name %>.<%= ThisTable.ForeignKeys[i].ForeignKeyMemberColumns[0].Name %> <%}%> <%}%> 7. 遍历Table的FK(主键表)(以1:1为例) <% for(int i=0;i<ThisTable.PrimaryKeys.Count;i++){%> //其他表从键<%= i %>:<%= ThisTable.PrimaryKeys[i].Name %> //外键<%= i %>对应的列 <% for(int j=0;j<ThisTable.PrimaryKeys[i].PrimaryKeyMemberColumns.Count;j++){%> <%= ThisTable.PrimaryKeys[i].ForeignKeyTable.Name %>.<%= ThisTable.PrimaryKeys[i].ForeignKeyMemberColumns[0].Name %> <%}%> <%}%> 8. 添加选择目录输入参数 private string templateOutputDirectory=""; [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),typeof(System.Drawing.Design.UITypeEditor))] [Optional,NotChecked] [Category("OutputInfo")] [Description("输出结果目录")] [DefaultValue("")] public string OutputDirectory { get{ if(string.IsNullOrEmpty(templateOutputDirectory)) { return "C:\\"+(ThisDatabase!=null?ThisDatabase.Name:"Output"); } else { return templateOutputDirectory; } } set{ if(value.EndsWith("\\") value=value.Substring(0,value.Length-1); templateOutputDirectory=value; } } 9. 添加选择文件的输入参数 private string templateOutputFile; [Editor(typeof(System.Windows.Forms.Design.FileNameEditor),typeof(System.Drawing.Design.UITypeEditor))] [Optional,NotChecked] [Category("OutputInfo")] [Description("输出文件")] [DefaultValue("")] public string OutputFile { get{ if(string.IsNullOrEmpty(templateOutputFile)) { return "c:\\"+(ThisDatabase!=null?ThisDatabase.Name+".cs":"Output.cs"); } else{ return templateOutputFile; } } set{ templateOutputFile=value; } } 或者 <script runat="template"> //生成实体Entity类 privatevoid GenerateEntityClasses() { CodeTemplate Template =new EntityTemplate(); foreach(TableSchema table inthis.SourceDatabase.Tables) { string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs"; //生成模板 Template.SetProperty("Table",table); //文件输出 Template.RenderToFile(FileDirectory,true); Debug.WriteLine(FileDirectory +" 创建成功."); } } </script> 10. 数据库类型转换成C#类型的函数 public string DataType2CSharpType(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"; case DbType.Byte: return "byte"; case DbType.Currency: return "decimal"; case DbType.Date: return "DateTime"; case DbType.DateTime: return "DateTime"; case DbType.DateTime2: return "DateTime"; case DbType.DateTimeOffset: return "DateTime"; case DbType.Decimal: return "decimal"; case DbType.Double: return "double"; case DbType.Guid: return "Guid"; case DbType.Int16: return "short"; case DbType.Int32: return "int"; case DbType.Int64: return "long"; case DbType.Object: return "object"; case DbType.SByte: return "sbyte"; case DbType.Single: return "float"; case DbType.String: return "string"; case DbType.StringFixedLength: return "string"; case DbType.Time: return "DateTime"; case DbType.UInt16: return "ushort"; case DbType.UInt32: return "uint"; case DbType.UInt64: return "ulong"; case DbType.VarNumeric: return "decimal"; case DbType.Xml: return "string"; default: return "object"; } } 11. 获取数据库类型的字段在C#中的默认值 public string DataTypeDefaultValue(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "String.Empty"; case DbType.AnsiStringFixedLength: return "String.Empty"; case DbType.Binary: //Answer modified was just 0 return "new byte[] {}"; case DbType.Boolean: return "false"; case DbType.Byte: //Answer modified was just 0 return "(byte)0"; case DbType.Currency: return "0"; case DbType.Date: return "DateTime.MinValue"; case DbType.DateTime: return "DateTime.MinValue"; case DbType.DateTime2: return "DateTime.MinValue"; case DbType.DateTimeOffset: return "DateTime.MinValue"; case DbType.Decimal: return "0.0m"; case DbType.Double: return "0.0f"; case DbType.Guid: return "Guid.Empty"; case DbType.Int16: return "(short)0"; case DbType.Int32: return "(int)0"; case DbType.Int64: return "(long)0"; case DbType.Object: return "new object()"; case DbType.SByte: return "(sbyte)0"; case DbType.Single: return "0F"; case DbType.String: return "String.Empty"; case DbType.StringFixedLength: return "String.Empty"; case DbType.Time: return "new DateTime(1900,1,1,0,0,0,0)"; //return "DateTime.MaxValue"; case DbType.UInt16: return "(ushort)0"; case DbType.UInt32: return "(uint)0"; case DbType.UInt64: return "(ulong)0"; case DbType.VarNumeric: return "(decimal)0"; case DbType.Xml: return "String.Empty"; default: return "null"; } } 12. 文件输出函数 public override void Render(TextWriter writer) { if (!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); StreamWriter BaseFile = new StreamWriter(OutputFile, false); base.Render(writer); BaseFile.Close(); }