首页 技术 正文
技术 2022年11月16日
0 收藏 914 点赞 2,179 浏览 3235 个字

目前用到了EF5进行模型创建,发现从数据库生成过来的实体中并没有包含字段的说明信息(鄙视下微软,这么简单的问题都不给解决下,太粗枝大叶了),网上找到了EFTSQLDocumentation.Generator.exe的相关方法,但按照操作后,死活没有字段说明信息,只好看其源码,最终发现是xmlns的命名空间不对导致的,程序中给写死了,没法用。自己就哪来修改下,变成可视化直接操作的windows应用程序,分享与大家!

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
var connectionString = this.txtConnectionString.Text;
var fromEdmx = this.txtFrom.Text;
var toEdmx = this.txtTo.Text;
var nameSpace = this.txtNamespace.Text;
if (connectionString == "")
{
MessageBox.Show("请填写数据库链接字符串!");
return;
}
if (fromEdmx == "")
{
MessageBox.Show("请填写源Edmx文件的物理路径!");
return;
}
if (toEdmx == "")
{
MessageBox.Show("请填写新生成Edmx文件的物理路径!");
return;
}
if (nameSpace == "")
{
MessageBox.Show("请填写Edmx中的xmlns命名空间(每个版本可能不一样)!");
return;
}
try
{
var creater = new Creater(connectionString, fromEdmx, toEdmx, nameSpace);
creater.CreateDocumentation();
creater.Dispose();
MessageBox.Show("操作成功了,哥们!如何谢我?");
}
catch (Exception ex)
{
MessageBox.Show("对不住,兄弟,可能是你的错,也可能是我的错!参考下具体信息:" + ex.Message);
//throw;
} }
} public class Creater
{
public String ConnectionString { get; set; }
public String InputFileName { get; set; }
public String OutputFileName { get; set; }
public SqlConnection Connection { get; set; }
public String XmlnsNameSpace { get; set; } public Creater(String connectionString, String inputFileName, String outputFileName, string nameSpace)
{
this.ConnectionString = connectionString;
this.InputFileName = inputFileName;
this.OutputFileName = outputFileName;
this.XmlnsNameSpace = nameSpace;
this.Connection = new SqlConnection(connectionString);
this.Connection.Open();
}
public void Dispose()
{
this.Connection.Dispose();
}
public void CreateDocumentation()
{ var doc = XDocument.Load(this.InputFileName);
var entityTypeElements = doc.Descendants("{" + XmlnsNameSpace + "}EntityType"); var i = ;
foreach (var entityTypeElement in entityTypeElements)
{
var tableName = entityTypeElement.Attribute("Name").Value;
var propertyElements = entityTypeElement.Descendants("{" + XmlnsNameSpace + "}Property"); this.AddNodeDocumentation(entityTypeElement, GetTableDocumentation(tableName)); foreach (var propertyElement in propertyElements)
{
var columnName = propertyElement.Attribute("Name").Value;
this.AddNodeDocumentation(propertyElement, GetColumnDocumentation(tableName, columnName));
}
} if (File.Exists(this.OutputFileName))
File.Delete(this.OutputFileName);
doc.Save(this.OutputFileName);
}
private void AddNodeDocumentation(XElement element, String documentation)
{
if (String.IsNullOrEmpty(documentation))
return;
element.Descendants("{" + XmlnsNameSpace + "}Documentation").Remove(); element.AddFirst(new XElement("{" + XmlnsNameSpace + "}Documentation", new XElement("{" + XmlnsNameSpace + "}Summary", documentation)));
}
private String GetTableDocumentation(String tableName)
{
using (var command = new SqlCommand(@" SELECT [value]
FROM fn_listextendedproperty (
'MS_Description',
'schema', 'dbo',
'table', @TableName,
null, null)", this.Connection))
{ command.Parameters.AddWithValue("TableName", tableName); return command.ExecuteScalar() as String;
}
}
private String GetColumnDocumentation(String tableName, String columnName)
{
using (var command = new SqlCommand(@"SELECT [value]
FROM fn_listextendedproperty (
'MS_Description',
'schema', 'dbo',
'table', @TableName,
'column', @columnName)", this.Connection))
{ command.Parameters.AddWithValue("TableName", tableName);
command.Parameters.AddWithValue("ColumnName", columnName); return command.ExecuteScalar() as String;
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901