1 public partial class Form1 : Form
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7
8 private void button1_Click(object sender, EventArgs e)
9 {
10 var connectionString = this.txtConnectionString.Text;
11 var fromEdmx = this.txtFrom.Text;
12 var toEdmx = this.txtTo.Text;
13 var nameSpace = this.txtNamespace.Text;
14 if (connectionString == "")
15 {
16 MessageBox.Show("请填写数据库链接字符串!");
17 return;
18 }
19 if (fromEdmx == "")
20 {
21 MessageBox.Show("请填写源Edmx文件的物理路径!");
22 return;
23 }
24 if (toEdmx == "")
25 {
26 MessageBox.Show("请填写新生成Edmx文件的物理路径!");
27 return;
28 }
29 if (nameSpace == "")
30 {
31 MessageBox.Show("请填写Edmx中的xmlns命名空间(每个版本可能不一样)!");
32 return;
33 }
34 try
35 {
36 var creater = new Creater(connectionString, fromEdmx, toEdmx, nameSpace);
37 creater.CreateDocumentation();
38 creater.Dispose();
39 MessageBox.Show("操作成功了,哥们!如何谢我?");
40 }
41 catch (Exception ex)
42 {
43 MessageBox.Show("对不住,兄弟,可能是你的错,也可能是我的错!参考下具体信息:" + ex.Message);
44 //throw;
45 }
46
47 }
48 }
49
50 public class Creater
51 {
52 public String ConnectionString { get; set; }
53 public String InputFileName { get; set; }
54 public String OutputFileName { get; set; }
55 public SqlConnection Connection { get; set; }
56 public String XmlnsNameSpace { get; set; }
57
58
59 public Creater(String connectionString, String inputFileName, String outputFileName, string nameSpace)
60 {
61 this.ConnectionString = connectionString;
62 this.InputFileName = inputFileName;
63 this.OutputFileName = outputFileName;
64 this.XmlnsNameSpace = nameSpace;
65 this.Connection = new SqlConnection(connectionString);
66 this.Connection.Open();
67 }
68 public void Dispose()
69 {
70 this.Connection.Dispose();
71 }
72 public void CreateDocumentation()
73 {
74
75 var doc = XDocument.Load(this.InputFileName);
76 var entityTypeElements = doc.Descendants("{" + XmlnsNameSpace + "}EntityType");
77
78 var i = 0;
79 foreach (var entityTypeElement in entityTypeElements)
80 {
81 var tableName = entityTypeElement.Attribute("Name").Value;
82 var propertyElements = entityTypeElement.Descendants("{" + XmlnsNameSpace + "}Property");
83
84 this.AddNodeDocumentation(entityTypeElement, GetTableDocumentation(tableName));
85
86 foreach (var propertyElement in propertyElements)
87 {
88 var columnName = propertyElement.Attribute("Name").Value;
89 this.AddNodeDocumentation(propertyElement, GetColumnDocumentation(tableName, columnName));
90 }
91 }
92
93 if (File.Exists(this.OutputFileName))
94 File.Delete(this.OutputFileName);
95 doc.Save(this.OutputFileName);
96 }
97 private void AddNodeDocumentation(XElement element, String documentation)
98 {
99 if (String.IsNullOrEmpty(documentation))
100 return;
101 element.Descendants("{" + XmlnsNameSpace + "}Documentation").Remove();
102
103 element.AddFirst(new XElement("{" + XmlnsNameSpace + "}Documentation", new XElement("{" + XmlnsNameSpace + "}Summary", documentation)));
104 }
105 private String GetTableDocumentation(String tableName)
106 {
107 using (var command = new SqlCommand(@" SELECT [value]
108 FROM fn_listextendedproperty (
109 'MS_Description',
110 'schema', 'dbo',
111 'table', @TableName,
112 null, null)", this.Connection))
113 {
114
115 command.Parameters.AddWithValue("TableName", tableName);
116
117 return command.ExecuteScalar() as String;
118 }
119 }
120 private String GetColumnDocumentation(String tableName, String columnName)
121 {
122 using (var command = new SqlCommand(@"SELECT [value]
123 FROM fn_listextendedproperty (
124 'MS_Description',
125 'schema', 'dbo',
126 'table', @TableName,
127 'column', @columnName)", this.Connection))
128 {
129
130 command.Parameters.AddWithValue("TableName", tableName);
131 command.Parameters.AddWithValue("ColumnName", columnName);
132
133 return command.ExecuteScalar() as String;
134 }
135 }
136 }