为PetaPoco添加实体模板
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添加实体模板的更多相关文章
- 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能
转自http://jeffblog.sinaapp.com/archives/501 首先我们先下载一个文件GetSummery,这里我提供了,大家可以直接下载:下载 我们在数据库建立一个表,并给表中 ...
- Microsoft Visual Studio 2012 添加实体数据模型
Microsoft Visual Studio 2012 添加实体数据模型 1.创建一个web项目 2.添加ADO实体数据模型,如下图: 3.选择 从数据库生成,然后下一步 4.新建连接,如下图: ...
- 如何在Android Studio中添加注释模板信息?
如何在Android Studio中添加注释模板信息? 在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android ...
- ABP框架入门踩坑-添加实体
添加实体 ABP踩坑记录-目录 这里我以问答模块为例,记录一下我在创建实体类过程中碰到的一些坑. 审计属性 具体什么是审计属性我这里就不再介绍了,大家可以参考官方文档. 这里我是通过继承定义好的基类来 ...
- NetCore+Dapper WebApi架构搭建(三):添加实体和仓储
上一节讲了类库添加一些底层的基本封装,下面来添加实体和仓储 1.Entities文件夹添加一个实体类Users,继承BaseModel,即拥有BaseModel的主键 using System; na ...
- OFBiz:添加实体栏位
如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...
- CodeSmith单表生成实体模板与生成多表实体模板
生成单实体模板: <%@ Template Language="C#" TargetLanguage="C#" %> <%@ Assembly ...
- 004-linux下配置rsyslog日志收集服务器案例 rsyslog+loganalyzer日志服务器,无法添加报表模板解决
centos6系统 client1:192.168.1.33 centos7系统 client2:192.168.1.44 centos7系统 master:192.168.1.55 配置服务端mas ...
- Android Studio(六):Android Studio添加注释模板
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
随机推荐
- spring security 管理会话 多个用户不可以使用同一个账号登录系统
多个用户不能使用同一个账号同时登陆系统. 1. 添加监听器 在web.xml中添加一个监听器,这个监听器会在session创建和销毁的时候通知Spring Security. <listener ...
- Linux磁盘及文件系统管理
在LINUX中我们知道一个很重要的概念,那就是"一切皆文件",这里的一切表示所有在LINUX系统的对象,自然也包括了LINUX中的硬盘设备.在LINUX中所有设备都被抽象成一个文件 ...
- bzoj4457: 游戏任务--双层01背包
这题和NOIP的金明的预算方案(?)很像,只不过附件的数量增多了 如果对主件进行一次01背包,再套一层附件的01背包O(n4)肯定会爆.. 所以我们可以先预处理出,对于每个主件,花的时间为k的情况下, ...
- Android错误:W/ResourceType(2411): No package identifier when getting value for resource number 0x
报错信息: 07-04 11:14:43.064: W/ResourceType(2411): No package identifier when getting value for resourc ...
- ping过程
premise: 一个局域网内,网段为192.168.0.0 , 有两台主机A(192.168.0.3) 主机B(192.168.0.4) 1. A 机器上执行: ping 192.168.0.4 2 ...
- C# Datatable排序
在C#中要对Datatable排序,可使用DefaultView的Sort方法.先获取Datatable的DefaultView,然后设置 得到的Dataview的sort属性,最后用视图的ToTab ...
- ThinkPHP 3.2.3 多模块 和 多应用 的配置
多模块 在 ThinkPHP 3.2.3 中,默认的应用目录是 ./Application,下面的默认模块是 Home 模块,如果此时需要添加一个 Admin 模块用于后台应用,根据手册 http:/ ...
- 算法与数据结构题目的 PHP 实现:栈和队列 由两个栈组成的队列
思路:同样使用 PHP 的数组模拟栈.栈的特点是先进后出,队列的特点是先进先出,可以用第一个栈(StackPush)作为压入栈,压入数据的时候只往这个栈中压入数据,第二个栈作(StackPop)为弹出 ...
- Cocos2dx淌坑日记:粒子系统PositionType的正确使用
Cocos2dx中的粒子系统,有三种定位方式,对应于不同需求. 之前我有一个想做的效果,是类似彗星的扫尾.但是当父节点也就是CCLayer跟着物体移动的时候,发现尾巴并没有跟随CCLayer移动,而是 ...
- Composer : php依赖管理工具
原始时代 我记得在当时用php的时候还没有composer,只有个pear,但是不好用呀,还不如直接在互联网上到处复制代码了,更快更不容易出错,当时也没有github这么好的社区工具了 总结如下 代码 ...