codesmith生成SQLSERVER實體(帶註釋)

       記錄用codesmith生成SQLSERVER數據庫實體的一個模板,具體鏈接數據庫和使用方式,大家可以百度,有非常多的資料,只記錄一個模板:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%--聲明數據庫表的參數,在左下角的表屬性中,選擇要操作的數據庫表--%>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%--引入system類型轉爲c#的數據類型的映射字典 --%>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%--引入下面的類庫,操作數據庫必備的。--%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
using Dapper;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace test
{
    //<%= SourceTable.Description %>
    public class <%= SourceTable.Name %>
    {
        #region 數據庫字段
    <%--遍歷數據庫表的字段屬性--%>
    <% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
    <%--拼接字符串,輸出c#中實體的屬性--%>
        /// <summary>
        ///<%= column.Description %>
        /// </summary>
        public <%= ControlType(CSharpAlias[column.SystemType.FullName]) %> <%= column.Name %>{ get; set; }

        <% } %>
        #endregion

        #region 方法
        
        #endregion
    }
}
<script runat="template">
 //如果類型爲int,或datetime類型輸出可空類型
 public string ControlType(object val)
 {
     var ty=val.ToString();
     if(ty=="int")
     {
         return "int";
     }
     if(ty=="System.DateTime")
     {
         return "DateTime";
     }
     return ty;
 }
</script>

       涉及到表註釋和字段註釋,效果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章