在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<%-- Name:自动生成 Author: 陈胜威 Description: 直接生成model类 --%> <%@ Template Language="C#" TargetLanguage="cs" %> <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Default="" Optional="True" Category="Tables" Description="This is a sample string property." %> <%@ Property Name="ProjectName" Type="System.string" Default="Blog" Optional="False" Category="Strings" Description="This is Project Name" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Assembly Name="System.Data" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="System.Data" %> <%@ Map Name="SqlCSharp" Src="Sql-CSharp" Description="System to C# Type Map" %> <%@ Map Name="DbDataReader" Src="DbType-DataReaderMethod" Description="DbType to DataReader Method Map" %> <%@ Map Name="SqlNativeSqlDb" Src="SqlNativeType-SqlDbType" Description="SqlNativeType to SqlDbType Map" %> <%@ Map Name="DbTypeCSharp" Src="DbType-CSharp" Description="DbType to CSharp Map" %> using System; namespace <%= ProjectName%>.Model { public class <%= GetClassName(SourceTable) %> { <% foreach(var column in SourceTable.Columns) { %> <%=GetMemberVariableDeclarationStatement(column)%> <% } %> public <%= GetClassName(SourceTable) %>(<%=GetConStructorParameters(SourceTable) %>) { <% foreach(ColumnSchema column in SourceTable.Columns) { %> <%= GetMemberVariableName(column) %>=<%= GetMemberVariableName(column).Substring(1) %>; <% } %> } <% foreach(ColumnSchema column in SourceTable.Columns) { %> public <%=GetCSharpVariableType(column) %> <%=GetPascalMemberVariableName(column) %> { get { return <%=GetMemberVariableName(column) %>; } set { <%=GetMemberVariableName(column) %>=value; } } <% } %> } } <script runat="template"> public string GetConStructorParameters(TableSchema table) { string returnParameters=""; foreach(ColumnSchema column in table.Columns) { returnParameters+=GetCSharpVariableType(column)+" "+GetMemberVariableName(column).Substring(1)+","; } return returnParameters.Substring(0,returnParameters.Length-1); } public string GetMemberVariableDeclarationStatement(ColumnSchema column) { return GetMemberVariableDeclarationStatement("private", column); } public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column) { string statement = protectionLevel + " "; statement += GetCSharpVariableType(column) + " " + GetMemberVariableName(column); string defaultValue = GetMemberVariableDefaultValue(column); if (defaultValue != "") { statement += " = " + defaultValue; } statement += ";"; return statement; } public string GetReaderAssignmentStatement(ColumnSchema column, int index) { string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) "; statement += GetMemberVariableName(column) + " = "; if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")"; statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");"; return statement; } public string GetCamelCaseName(string value) { return value.Substring(0, 1).ToLower() + value.Substring(1); } public string GetMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); return memberVariableName; } public string GetPascalMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); string temp= propertyName.Substring(0,1).ToUpper()+propertyName.Substring(1); return temp; } public string GetPropertyName(ColumnSchema column) { string propertyName = column.Name; if (propertyName == column.Table.Name + "Name") return "Name"; if (propertyName == column.Table.Name + "Description") return "Description"; if (propertyName.EndsWith("TypeCode")) propertyName = propertyName.Substring(0, propertyName.Length - 4); return propertyName; } public string GetMemberVariableDefaultValue(ColumnSchema column) { switch (column.DataType) { case DbType.Guid: { return "Guid.Empty"; } case DbType.AnsiString: case DbType.AnsiStringFixedLength: case DbType.String: case DbType.StringFixedLength: { return "String.Empty"; } default: { return ""; } } } public string GetCSharpVariableType(ColumnSchema column) { if (column.Name.EndsWith("TypeCode")) return column.Name; return DbTypeCSharp[column.DataType.ToString()]; } public string GetReaderMethod(ColumnSchema column) { return DbDataReader[column.DataType.ToString()]; } public string GetClassName(TableSchema table) { if (table.Name.EndsWith("s")) { return table.Name.Substring(0, table.Name.Length - 1); } else { return table.Name; } } public string GetSqlDbType(ColumnSchema column) { return SqlNativeSqlDb[column.NativeType.ToString()]; } public string GetPrimaryKeyType(TableSchema table) { if (table.PrimaryKey != null) { if (table.PrimaryKey.MemberColumns.Count == 1) { return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]); } else { throw new ApplicationException("This template will not work on primary keys with more than one member column."); } } else { throw new ApplicationException("This template will only work on tables with a primary key."); } } public override string GetFileName() { return this.GetClassName(this.SourceTable) + ".cs"; } </script> |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论