关于Entity Framework 5 从数据库生成模型时没有字段注释的解决方法!

目前用到了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;
}
}
}
上一篇:Envelope几何对象 Curve对象几何对象 Multipatch几何对象 Geometry集合接口 IGeometryCollection接口


下一篇:matlab常用小函数(一)