T4模板-Model.cst

2 篇文章 0 订阅
<%-- 
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) { %>
        
        <% } %>
        <% } %>
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值