新建GetSummery.ttinclude
<#@ assembly name="System.Core" #> <#@ assembly name="System.Data" #> <#@ import namespace="System.Data.SqlClient" #> <#+ string constr = "Data Source=127.0.0.1;Initial Catalog=Workers;User ID=sa;Password=li123"; string getTableSummery(string tablename) { using (SqlConnection conn = new SqlConnection(constr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT value FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘"+ tablename+"‘, default, default)"; var res = cmd.ExecuteScalar(); if (res == null) { return string.Empty; } else { return res.ToString(); } } } } string getColumnSummery(string tablename, string columnname) { using (SqlConnection conn = new SqlConnection(constr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT value FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘" + tablename + "‘, ‘column‘, default) where objname=‘"+ columnname + "‘ "; var res = cmd.ExecuteScalar(); if (res == null) { return string.Empty; } else { return res.ToString(); } } } } #>
1,项目根目录增加 GetSummery.ttinclude
2, 修改model.tt,在顶部位置加入代码:
<#@ include file="GetSummery.ttinclude"#>
3,插入类注释 在代码片段<#=codeStringGenerator.EntityClassOpening(entity)#>的上方(28行左右)插入
/// <summary>
/// <#= getTableSummery(code.Escape(entity)) #>
/// </summary>
4,添加字段注释 在代码片段<#=codeStringGenerator.Property(edmProperty)#>的上方(76行左右)插入
///<summary>
///<#= getColumnSummery(code.Escape(entity),code.Escape(edmProperty)) #>
///</summary>
5,修改model.context.tt,在顶部位置加入代码:
<#@ include file="GetSummery.ttinclude"#>
在代码片段<#=codeStringGenerator.DbSet(entitySet)#>的上方插入
///<summary>
///<#= getTableSummery(code.Escape(entitySet)) #>
///</summary>