首页 > 代码库 > petapoco-SQLServer模型增加注释
petapoco-SQLServer模型增加注释
petapoco 是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了
获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description
technet 参考资料:sys.fn_listextendedproperty (Transact-SQL)
一、首先是 PetaPoco.Core.ttinclude
1、106行原始:
public string SequenceName;public bool Ignore;
新建Description字段,这个是表的注释
public string SequenceName;public bool Ignore;public string Description;
2、140行原始:
public string SequenceName;public bool Ignore;
新建Description字段,这个是列的注释
public string SequenceName;public bool Ignore;public string Description;
3、517行原始:
foreach (var tbl in result){ tbl.Columns=LoadColumns(tbl); // Mark the primary key string PrimaryKey=GetPK(tbl.Name); var pkColumn=tbl.Columns.SingleOrDefault(x=>x.Name.ToLower().Trim()==PrimaryKey.ToLower().Trim()); if(pkColumn!=null) { pkColumn.IsPK=true; }}
修改后,调用函数获取表注释:
foreach (var tbl in result){ using(var cmdDesc=_factory.CreateCommand()) { cmdDesc.Connection = connection; cmdDesc.CommandText = TABLE_DESC_SQL; DbParameter p = null; p = cmdDesc.CreateParameter(); p.ParameterName = "@schema"; p.Value = tbl.Schema; cmdDesc.Parameters.Add(p); p = cmdDesc.CreateParameter(); p.ParameterName = "@table"; p.Value = tbl.Name; cmdDesc.Parameters.Add(p); using (var rdrDesc = cmdDesc.ExecuteReader()) { if (rdrDesc.Read()) tbl.Description = rdrDesc["value"].ToString(); } } tbl.Columns=LoadColumns(tbl); // Mark the primary key string PrimaryKey=GetPK(tbl.Name); var pkColumn=tbl.Columns.SingleOrDefault(x=>x.Name.ToLower().Trim()==PrimaryKey.ToLower().Trim()); if(pkColumn!=null) { pkColumn.IsPK=true; }}
4、572行原始,插入新代码,获取每一列的注释(return result 上面):
foreach (var col in result){ using (var cmdDesc = _factory.CreateCommand()) { cmdDesc.Connection = _connection; cmdDesc.CommandText = COLUMN_DESC_SQL; DbParameter pDesc = null; pDesc = cmdDesc.CreateParameter(); pDesc.ParameterName = "@schema"; pDesc.Value = tbl.Schema; cmdDesc.Parameters.Add(pDesc); pDesc = cmdDesc.CreateParameter(); pDesc.ParameterName = "@table"; pDesc.Value = tbl.Name; cmdDesc.Parameters.Add(pDesc); pDesc = cmdDesc.CreateParameter(); pDesc.ParameterName = "@column"; pDesc.Value = col.Name; cmdDesc.Parameters.Add(pDesc); using (var rdrDesc = cmdDesc.ExecuteReader()) { if (rdrDesc.Read()) col.Description = rdrDesc["value"].ToString(); } }}
5、672、688行原始,插入新的代码,存储调用函数的sql语句:
const string TABLE_DESC_SQL = @"SELECT value FROM ::fn_listextendedproperty(‘MS_Description‘, ‘user‘, @schema, ‘table‘, @table, null, null)";const string COLUMN_DESC_SQL = @"SELECT value FROM ::fn_listextendedproperty(‘MS_Description‘, ‘user‘, @schema, ‘table‘, @table, ‘column‘, @column)";
至此已经从数据库获取到了注释,下面需要将注释插入到T4模板中!
二、修改PetaPoco.Generator.ttinclude
1、141行原始:
<#foreach(Table tbl in from t in tables where !t.Ignore select t){#> <# if (string.IsNullOrEmpty(tbl.Schema)) { #> [TableName("<#=tbl.Name#>")]<# } else { #>
添加表的 Description 注释:
<#foreach(Table tbl in from t in tables where !t.Ignore select t){#> /// <summary> /// <#=tbl.Description??""#> /// </summary> <# if (string.IsNullOrEmpty(tbl.Schema)) { #> [TableName("<#=tbl.Name#>")]<# } else { #>
2、167行原始:
<#foreach(Column col in from c in tbl.Columns where !c.Ignore select c){ // Column bindings#><# if (TrackModifiedColumns) { #>
添加列的 Description 注释:
<#foreach(Column col in from c in tbl.Columns where !c.Ignore select c){ // Column bindings#> /// <summary> /// <#=col.Description??""#> /// </summary><# if (TrackModifiedColumns) { #>
这样就改完了,打开database.tt按ctrl+s就能更新获取到的注释了!
另外GetInstance居然不是单例方法,这简直没法忍,果断改掉:
PetaPoco.Generator.ttinclude,38行修改为:
public static <#=RepoName#> GetInstance(){ if (_instance!=null) return _instance; if (Factory!=null) return Factory.GetInstance(); //else // return new <#=RepoName#>(); return _instance = new <#=RepoName#>();}
petapoco-SQLServer模型增加注释
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。