记录用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>
涉及到表注释和字段注释,效果如下: