<%--
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>

codesmith 自动生成C# model 实体模板的更多相关文章

  1. CodeSmith单表生成实体模板与生成多表实体模板

    生成单实体模板: <%@ Template Language="C#" TargetLanguage="C#" %> <%@ Assembly ...

  2. SqlServer数据库表生成C# Model实体类SQL语句——补充

    在sql语句最前边加上  use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html   --[SQL骚操作]SqlServer数据库表生成C ...

  3. 使用generator自动生成mybatis model、mapper.xml、mapper等(转)

    原文链接:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件 ...

  4. 如何由XSD自动生成XML和实体类

    项目中有时候要用XML作为数据源,因此需要定义XML文件和相应的类,最佳方法是首先定义XSD,然后自动生成实体类,最后生成XML和填充数据:读取XML数据源的时候,首先用XSD验证XML数据格式,然后 ...

  5. MyEclipse10中自动生成Hibernate的实体和xml配置文件

    前提:1.在项目中添加Hibernate支持 2.MyEclipse中已经创建好数据库连接 3.表已经建好并且有主键 步骤如下: 1.在DB Browser窗口的已打开连接节点中选中用户创建的所有的表 ...

  6. 根据现有的数据库自动生成Django model

    Django引入外部数据库还是比较方便的,首先在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自动生成models模型文件 python ma ...

  7. Yii2用Gii自动生成Module+Model+CRUD

    1. 开启gii模块 common/config/main-local.php加入下面代码 return [ 'modules' => [ 'gii' => [ 'class' => ...

  8. idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

    1.集成到开发环境中 以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins></plugins>中间 ...

  9. Java工具类_表结构自动生成对应的实体类、Mapper.xml文件、Dao类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

随机推荐

  1. 【动态规划】【二分】CDOJ1006 最长上升子序列

    最长上升子序列. 要求输出字典序最小解. 就在更新答案的时候记录一下前驱.容易发现记录的这个玩意实际上形成了一个森林. #include<cstdio> #include<algor ...

  2. Problem K: 零起点学算法107——统计元音

    #include<stdio.h> int main() { int n; ]; while(scanf("%d%*c",&n)!=EOF) { while(n ...

  3. Windows下编译protobuf v3.3.0

    一:概述 关于 protobuf 在此不再多说,此处记录下成功编译步骤以备日后查阅.注意:本文并不是使用cmake gui进行编译的,如果熟悉cmake gui的话,也可以使用gui进行生成编译. 二 ...

  4. linux 配置信息

    # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...

  5. css3背景属性 background-size 对背景图进行缩小放大

    background-size需要两个值,它的类型可以是像素(px).百分比(%)或是auto,还可以是cover和contain.第一个值为背景图的width,另外一个值用于指定背景图上的heigh ...

  6. opencv中keypoint数据结构分析

    分析opencv中keypoint数据结构的相关信息,找到opencv的document(http://docs.opencv.org/java/org/opencv/features2d/KeyPo ...

  7. Android Toolbar返回按钮颜色修改

    // 代码设置toolbar返回键颜色为白色 val upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mater ...

  8. Do waiting or suspended tasks tie up a worker thread?

      https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...

  9. TSQL语言基础笔记之单表查询

    db_id()函数 返回对象的id,如果返回的对象为null,则判断不存在 if db_id('testdb') is null --判断数据库是否存在 create database testdb ...

  10. svn: None of the environment variables SVN_EDITOR...问题解决

    转:http://blog.163.com/lgh_2002/blog/static/44017526201046111856208/ 问题1: svn: Could not use external ...