Brad为我们提供了T4模板,因为公司一直在使用CodeSmith,故为其写了一个CodeSmith的模板,代码如下:

<%--
Name:EntityTemplates
Author:
Description:Generate a entity file in C#
--%> <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="" ResponseEncoding="UTF-8" %>
<%@ Property Name="Namespace" Type="System.String" Default="TianChenMeiKuang.Entity" Optional="False" Category="Strings" Description="实体类命名空间" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="源表" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
/**********************************************************
Name:<%= GetClassName(SourceTable) %>
Author:
Date:<%=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") %>
Description:
Modify Remark:
**********************************************************/
using System;
using WebApp.Matrix.Data; namespace <%=Namespace%>
{
/// <summary>
/// This Entity is Mapping To [<%=SourceTable.Name%>] Table
/// Remark Ignore Attribute for the field when it is not need mapping
/// </summary>
[Serializable]
[TableName("[<%=SourceTable.Name%>]")]
<%
ColumnSchema primaryKeyColumn = GetPrimaryKeyColumn();
if(primaryKeyColumn != null)
{
if(Convert.ToBoolean(primaryKeyColumn.ExtendedProperties["CS_isIdentity"].Value)==true){ %>
[PrimaryKey("<%=primaryKeyColumn.Name%>", autoIncrement=true)]
<% }
else {
%>
[PrimaryKey("<%=primaryKeyColumn.Name%>", autoIncrement=false)]
<% }
}%>
public class <%= GetClassName(SourceTable) %>
{
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
/// <summary>
/// <%= SourceTable.Columns[i].Name %>
/// </summary>
public <%= GetCSharpVariableType(SourceTable.Columns[i]) %> <%= GetPropertyName(SourceTable.Columns[i]) %>
{
get; set;
}
<% if (i < SourceTable.Columns.Count - 1) Response.Write("\r\n"); %>
<% } %> /// <summary>
/// Equals
/// </summary>
public override bool Equals(object obj)
{
<%= GetClassName(SourceTable) %> other = obj as <%= GetClassName(SourceTable) %>;
if (<%=GetFirstKeyCondition()%>)
{
return false;
}
if (<%=GetTwoKeyCondition()%>)
{
return false;
}
return true;
}
/// <summary>
/// GetHashCode
/// </summary>
public override int GetHashCode()
{
return base.GetHashCode();
}
}
} <script runat="template">
public string GetClassName(TableSchema table)
{
string className = table.Name;
return className;
}
public string GetPropertyName(ColumnSchema column)
{
string propertyName = column.Name; return propertyName;
}
public string GetFieldName(ColumnSchema column)
{
string propertyName = column.Name;
return propertyName;
}
public ColumnSchema GetPrimaryKeyColumn()
{
for (int i = 0; i < SourceTable.Columns.Count; i++)
{
if(SourceTable.Columns[i].IsPrimaryKeyMember)
{
return SourceTable.Columns[i];
}
}
return null;
}
public string GetKey()
{
for (int i = 0; i < SourceTable.Columns.Count; i++)
{
if(SourceTable.Columns[i].IsPrimaryKeyMember)
{
return SourceTable.Columns[i].Name;
}
}
return "GetHashCode().ToString()";
}
public string GetCSharpVariableType(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.AnsiString: return "string";
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "Nullable<int>";
case DbType.Boolean: if(column.AllowDBNull){return "Nullable<bool>";}else{return "bool";};
case DbType.Byte: return "int";
case DbType.Currency: if(column.AllowDBNull){return "Nullable<decimal>";}else{return "decimal";};
case DbType.Date: if(column.AllowDBNull){return "Nullable<DateTime>";}else{return "DateTime";};
case DbType.DateTime: if(column.AllowDBNull){return "Nullable<DateTime>";}else{return "DateTime";};
case DbType.Decimal: if(column.AllowDBNull){return "Nullable<decimal>";}else{return "decimal";};
case DbType.Double: if(column.AllowDBNull){return "Nullable<double>";}else{return "double";};
case DbType.Guid: return "Guid";
case DbType.Int16: if(column.AllowDBNull){return "Nullable<short>";}else{return "short";};
case DbType.Int32: if(column.AllowDBNull){return "Nullable<int>";}else{return "int";};
case DbType.Int64: if(column.AllowDBNull){return "Nullable<long>";}else{return "long";};
case DbType.Object: return "object";
case DbType.SByte: if(column.AllowDBNull){return "Nullable<sbyte>";}else{return "sbyte";};
case DbType.Single: if(column.AllowDBNull){return "Nullable<float>";}else{return "float";};
case DbType.String: return "string";
case DbType.StringFixedLength: return "string";
case DbType.Time: if(column.AllowDBNull){return "Nullable<TimeSpan>";}else{return "TimeSpan";};
case DbType.UInt16: if(column.AllowDBNull){return "Nullable<ushort>";}else{return "ushort";};
case DbType.UInt32: if(column.AllowDBNull){return "Nullable<uint>";}else{return "uint";};
case DbType.UInt64: if(column.AllowDBNull){return "Nullable<ulong>";}else{return "ulong";};
case DbType.VarNumeric: if(column.AllowDBNull){return "Nullable<decimal>";}else{return "decimal";};
default:return "string";
}
}
public string GetFirstKeyCondition()
{
string condition = " other==null";
for (int i = 0; i < SourceTable.Columns.Count; i++)
{
var tempColumn = SourceTable.Columns[i];
if(tempColumn.IsPrimaryKeyMember)
{
if(((bool)tempColumn.ExtendedProperties["CS_IsIdentity"].Value))
{
condition += " || this." + SourceTable.Columns[i].Name + " == 0";
condition += " || other."+ SourceTable.Columns[i].Name + " == 0";
}
else
{
condition += " || string.IsNullOrEmpty(this." + SourceTable.Columns[i].Name + ")";
condition += " || string.IsNullOrEmpty(other."+ SourceTable.Columns[i].Name + ")";
}
}
}
return condition;
}
public string GetTwoKeyCondition()
{
string condition = " ";
for (int i = 0; i < SourceTable.Columns.Count; i++)
{
if(SourceTable.Columns[i].IsPrimaryKeyMember)
{
condition += " || this."+SourceTable.Columns[i].Name +" !=other."+SourceTable.Columns[i].Name;
}
}
return condition.Substring(4,condition.Length-4).ToString();
}
public string GetHashCodeStr()
{
string hashCode = " ";
for (int i = 0; i < SourceTable.Columns.Count; i++)
{
if(SourceTable.Columns[i].IsPrimaryKeyMember)
{
hashCode += "+\"|\" +this."+SourceTable.Columns[i].Name +".ToLower()";
}
}
return hashCode.Substring(7,hashCode.Length-7).ToString();
}
public string GetDefaultValue(ColumnSchema column)
{
string DefaultValue = "";
switch(column.DataType)
{
case DbType.Int16:
case DbType.Int32:
case DbType.Int64:
DefaultValue = "0";
break;
case DbType.Decimal:
DefaultValue = "0.000000M";
break;
case DbType.AnsiString:
case DbType.String:
case DbType.StringFixedLength:
DefaultValue = "\"\"";
break;
case DbType.Date:
case DbType.DateTime:
case DbType.DateTime2:
DefaultValue = "DateTime.Parse(\"1999-01-01 00:00:00\")";
break;
case DbType.Binary:
DefaultValue = "new byte[] { }";
break;
case DbType.Boolean:
DefaultValue = "False";
break;
case DbType.Byte:
DefaultValue = "1";
break;
default:
break;
}
return DefaultValue;
}
</script>

该模板只适用于但主键的环境,且主键必须为字符串类型,或者为自增长列。

来源:http://www.cnblogs.com/youring2

为PetaPoco添加实体模板的更多相关文章

  1. 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能

    转自http://jeffblog.sinaapp.com/archives/501 首先我们先下载一个文件GetSummery,这里我提供了,大家可以直接下载:下载 我们在数据库建立一个表,并给表中 ...

  2. Microsoft Visual Studio 2012 添加实体数据模型

     Microsoft Visual Studio 2012 添加实体数据模型 1.创建一个web项目 2.添加ADO实体数据模型,如下图: 3.选择 从数据库生成,然后下一步 4.新建连接,如下图: ...

  3. 如何在Android Studio中添加注释模板信息?

    如何在Android Studio中添加注释模板信息? 在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android ...

  4. ABP框架入门踩坑-添加实体

    添加实体 ABP踩坑记录-目录 这里我以问答模块为例,记录一下我在创建实体类过程中碰到的一些坑. 审计属性 具体什么是审计属性我这里就不再介绍了,大家可以参考官方文档. 这里我是通过继承定义好的基类来 ...

  5. NetCore+Dapper WebApi架构搭建(三):添加实体和仓储

    上一节讲了类库添加一些底层的基本封装,下面来添加实体和仓储 1.Entities文件夹添加一个实体类Users,继承BaseModel,即拥有BaseModel的主键 using System; na ...

  6. OFBiz:添加实体栏位

    如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...

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

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

  8. 004-linux下配置rsyslog日志收集服务器案例 rsyslog+loganalyzer日志服务器,无法添加报表模板解决

    centos6系统 client1:192.168.1.33 centos7系统 client2:192.168.1.44 centos7系统 master:192.168.1.55 配置服务端mas ...

  9. Android Studio(六):Android Studio添加注释模板

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

随机推荐

  1. CSS3初学篇章_4(边框样式/段落样式)

    边框样式 1.边框线语法:border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inse ...

  2. 使用javascript实现在页面打印的效果的三种方式

    <div id="console"></div> <script type="text/javascript"> var c ...

  3. Mac 系统下cocos2dx 环境变量设置

    Mac 系统环境变量设置   vim ~/.bash_profile    export PATH=$PATH:/Users/wangchengcheng/Downloads/LearningSoft ...

  4. 20145337实验四Android开发基础

    20145337实验四Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件处 ...

  5. xml、文件操作功能类

    我一个项目中用到的,里面的方法不是太通用,但是可以从里面找到一些有用的代码,以后慢慢添补更新: FileUtil.xml package com.novel.util; import java.io. ...

  6. js中特有语句-with

    <script type="text/javascript"> /* *为了简化对象调用内容的书写. *可以用js中的特有语句with来完成. *格式 *with{ * ...

  7. DropDownList 绑定DataTable并给默认值

    string sql = @"SELECT FG_Id, FG_Name, FG_Sort, FG_IsDel FROM dbo.FirstGestation";          ...

  8. hgrjhgkjh

    #include<stdio.h> int step[5]={13,5,1,4,11}; int sum; int min=999; void ji() {  int i;  int j; ...

  9. Android实现推送方式解决方案

    当我们开发需要和服务器交互的应用程序时,基本上都需要获取服务器端的数据,比如<地震应急通>就需要及时获取服务器上最新的地震信息. 要获取服务器上不定时更新的信息,一般来说有两种方法: 第一 ...

  10. smarty模板原理和增删改查例题

    Smarty模板:(前后端分离)原理:核心是一个类,先修改配置文件,在使用的时候引入配置文件即可,(init.inc.php)$smarty->assign("ceshi", ...