petapoco-SQLServer模型增加注释
2017-08-10 00:14
162 查看
petapoco 是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了
获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description
technet 参考资料:sys.fn_listextendedproperty (Transact-SQL)
一、首先是 PetaPoco.Core.ttinclude
1、106行原始:
新建Description字段,这个是表的注释
2、140行原始:
新建Description字段,这个是列的注释
3、517行原始:
修改后,调用函数获取表注释:
4、572行原始,插入新代码,获取每一列的注释(return result 上面):
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810001929261-1441883945.png)
5、672、688行原始,插入新的代码,存储调用函数的sql语句:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810001942652-537876370.png)
至此已经从数据库获取到了注释,下面需要将注释插入到T4模板中!
二、修改PetaPoco.Generator.ttinclude
1、141行原始:
添加表的 Description 注释:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810002405324-1273165934.png)
2、167行原始:
添加列的 Description 注释:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810002417324-2093163732.png)
这样就改完了,打开database.tt按ctrl+s就能更新获取到的注释了!
另外GetInstance居然不是单例方法,这简直没法忍,果断改掉:
PetaPoco.Generator.ttinclude,38行修改为:
获取注释基本原理是调用数据库::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 上面):
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810001929261-1441883945.png)
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语句:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810001942652-537876370.png)
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 注释:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810002405324-1273165934.png)
<# 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 注释:
![](https://images2017.cnblogs.com/blog/552971/201708/552971-20170810002417324-2093163732.png)
<# 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#>(); }
相关文章推荐
- sqlServer增加一字段并给此字段添加注释
- sqlserver 对列增加注释
- SQLSERVER 对于非dbo的表增加注释
- sqlServer增加一字段并给此字段添加注释
- Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序
- 给ef6的实体模型 edmx文件加字段注释
- 解决PowerDesigner 反向工程没有列注释(SQLSERVER 2005版)
- Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结
- 使用PowerDesigner15设计表模型的时候,如何给各个字段添加注释。
- 在ORACLE中给表、列增加注释以及读取注释
- sqlserver 批量增加数据
- Oracle 增删列,为列增加注释,修改主键等
- 使用Javascript动态增加,删除表格(使用W3C对象模型)
- PowerDesigner 怎么给 Table Properties 增加注释和默认值
- socket select 模型(详细注释版本)
- VS2013修改模板,增加类文件的头注释
- PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
- 如何利用漏斗模型增加转化率减少流失率?
- Oracle 增加、修改、删除字段与添加注释
- 3DSlicer3:模块管理(一)颜色、DCM、数据、模型、注释