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. Eclipse序列号生成代码

    import java.io.*; public class MyEclipseGen { private static final String LL = "Decompiling thi ...

  2. Educational Codeforces Round 16 A B C E

    做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...

  3. 预防 Session 劫持与 Session 固定攻击

    一.预防 Session 劫持 要求: ① 只允许通过 Cookie 来传递 SessionID ② 生成一个由 URL 传递的唯一标识作为 Session 的标记(token) 当请求同时包含有效的 ...

  4. 滚动固定TAB在顶部显示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. 20145224&20145238《信息安全系统设计基础》实验一 开发环境的熟悉

    20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第一次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...

  6. CDN缓存那些事

    CDN是什么? 谈到CDN的作用,可以用8年买火车票的经历来形象比喻: 8年前,还没有火车票代售点一说,12306.cn更是无从说起.那时候火车票还只能在火车站的售票大厅购买,而我所住的小县城并不通火 ...

  7. 《Linux内核分析》第三周 构建一个简单的Linux系统MenuOS

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK THREE ...

  8. Geolocation

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. oracle从零开始学习笔记 二

    多表查询 等值连接(Equijoin) select ename,empno,sal,emp.deptno from emp,dept where dept.deptno=emp.deptno; 非等 ...

  10. node + nginx + mongo搭建负载均衡

    基于node和nignx和mongo搭建负载均衡 nginx配置: upstream back {                                                  # ...