<%--
Name: 数据实体模板
Author: lizhongxiang
Description: 根据数据库表生成数据实体层
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="AssemblyName" Type="String" Optional="True" Category="Context" Description="程序集名称" %>
<%@ Property Name="Author" Type="String" Optional="True" Default="lizhongxiang" Category="Context" Description="编写此模块的作者" %>
<%@ Property Name="CreateMapping" Type="Boolean" Optional="True" Default="false" Category="Context" Description="是否创建 Mapping 特性" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="待生成实体的表名" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
/**
* 名称: <%= this.SourceTable.Name %>
* 作者: <%= Author %>
* 时间: <%= DateTime.Now.ToString("yyyy-MM-dd") %>
* 版本: 1
* 说明: <%= string.IsNullOrEmpty(this.SourceTable.Description) ? this.SourceTable.Name : this.SourceTable.Description %> 数据实体类
*
* 历史:
* 版本<%= "\t\t"%>时间<%= "\t\t\t"%>修改人<%= "\t\t"%>说明
* 1<%= "\t\t"%><%= DateTime.Now.ToString("yyyy-MM-dd") %><%= "\t\t"%><%= Author %><%= "\t\t"%>创建本文件
*/
using System;
<% if (CreateMapping) { %>
using NPoco;
<% } %>
namespace <%= string.IsNullOrEmpty(AssemblyName) ? this.SourceTable.Database.Name : AssemblyName %>.Model
{
/// <summary>
/// <%= string.IsNullOrEmpty(this.SourceTable.Description) ? this.SourceTable.Name : this.SourceTable.Description %> 数据实体类
/// </summary>
<% if (CreateMapping) { %>
<%--[TableName("<%= this.SourceTable.Name %>")]--%>
<% if (this.SourceTable.HasPrimaryKey && this.SourceTable.PrimaryKey.MemberColumns.Count > 0) {
bool isIdentity = false;
int identitySeed = 0;
int identityIncrement = 0;
ColumnSchema column = this.SourceTable.PrimaryKey.MemberColumns[0];
foreach (ExtendedProperty item in column.ExtendedProperties) {
if (item.Name.Equals("CS_IsIdentity")) {
isIdentity = Convert.ToBoolean(item.Value);
} else if (item.Name.Equals("CS_IdentitySeed")) {
identitySeed = Convert.ToInt32(item.Value);
} else if (item.Name.Equals("CS_IdentityIncrement")) {
identityIncrement = Convert.ToInt32(item.Value);
}
}
%>
[PrimaryKey("<%= column.Name %>", AutoIncrement = <%= (isIdentity && identitySeed > 0 && identityIncrement > 0).ToString().ToLower() %>)]
<% } %>
<% } %>
public class <%= this.SourceTable.Name %>
{
<% for (int i=0; i<this.SourceTable.Columns.Count; i++) { %>
<% ColumnSchema column = this.SourceTable.Columns[i]; %>
/// <summary>
/// <%= string.IsNullOrEmpty(column.Description) ? column.Name : column.Description %>
/// </summary>
<% if (CreateMapping) { %>
[Column("<%= column.Name %>")]
<% } %>
<% string typeName = CSharpAlias[column.SystemType.FullName].Replace("System.", ""); %>
<% if (column.AllowDBNull && !column.SystemType.FullName.Equals("System.String")) { %>
public <%= typeName + "?" %> <%= column.Name %> { get; set; }
<% } else { %>
public <%= typeName %> <%= column.Name %> { get; set; }
<% } %>
<% if (i != this.SourceTable.Columns.Count - 1) { %>
<% } %>
<% } %>
}
}