Add field comments to the ef6 entity model edmx file, ef6edmx
Code smith template (to download the code smith software ):
// Generate comments part of the code
<% -- Name: edmx file with annotation Author: pukuiminDescription: -- %> <% @ Template Language = "C #" TargetLanguage = "XML" %> <% @ Assembly Name = "SchemaExplorer" %> <% @ Import Namespace = "SchemaExplorer" %> <% @ Import Namespace = "System. globalization "%> <% @ Import Namespace =" System. collections. generic "%> <% @ Map Name =" CSharpAlias "Src =" System-CSharpAlias "Description =" ing table of database field type and C # type "%> <% -- Database -- %> <% @ Property Name = "SourceD Atabase "Type =" SchemaExplorer. databaseSchema "DeepLoad =" True "Optional =" False "Category =" required Database "Description =" Database "%> <% -- table to be printed -- %> <% @ Property name = "Table" Type = "SchemaExplorer. tableSchema "DeepLoad =" True "Optional =" False "Category =" Required data table "Description =" table. "%> <% @ Property Name =" IsAll "Type =" System. boolean "Optional =" False "Category =" whether to generate all tables "Default =" True "Description =" whether to generate all tables "%> <%/* f Or (int I = TableName. length-1; I> = 0; I --) {if (TableName [I]> = 'A' & TableName [I] <= 'Z ') {TableName = TableName. substring (0, I) + TableName. substring (I, 1 ). toLower () + TableName. substring (I + 1); break;} */string primaryname = Table. primaryKey. memberColumns [0]. name; string primarytype = CSharpAlias [Table. primaryKey. memberColumns [0]. systemType. fullName]; string autoname = "", autotype = ""; foreach (ColumnSchema cs in Table. Columns) {if (bool) cs. extendedProperties ["CS_IsIdentity"]. value) = true) {autoname = cs. name; autotype = CSharpAlias [cs. systemType. fullName]; break ;}list <string> fkeys = new List <string> (); // store the foreign key column and the corresponding primary key Table name foreach (var item in Table. foreignKeys) {fkeys. add (item. foreignKeyMemberColumns [0]. name); // Response. write (item. foreignKeyMemberColumns [0]. name + "--" + item. primaryKey. table. name + "\ n");} TableSchema Collection tables = new TableSchemaCollection (); if (IsAll! = True) {tables. add (Table);} else {tables = this. sourceDatabase. tables ;}%> <% // if (tables = null) {Response. writeLine ("null"); return;} // else {Response. writeLine (tables. count) ;}%> <Schema> <! -- Manual modify begin --> <! -- Replace edmx in THE edmx file: content corresponding to the Schema in ConceptualModels and before EntityContainer --> <% foreach (TableSchema table in tables) {string TableName = table. name; %> <EntityType Name = "<% = TableName %>"> <Documentation> <Summary> <% = table. description %> </Summary> </Documentation> <Key> <PropertyRef Name = "<% = primaryname %>"/> </Key> <% for (int I = 0; I <table. columns. count; I ++) {ColumnSchema col = table. columns [I]; // string l Owertype = CSharpAlias [col. SystemType. FullName]; string type = col. SystemType. FullName. Replace ("System.", ""); string Nullable = col. AllowDBNull? "": "Nullable = \" false \ ""; // can be empty string maxlength = (type = "String "? "MaxLength = \" "+ col. size + "\" ":" "); // string maximum length string unicode = (col. dataType. toString () = "AnsiString "? "Unicode = \" false \ "" :( col. DataType. ToString () = "String "? "Unicode = \" true \ "": ""); // whether Chinese string fixedlength = "" can be stored; // whether the length is fixed if (col. nativeType = "nchar" | col. nativeType = "char") fixedlength = "FixedLength = \" true \ ""; // col. nativeType is the else if (col. nativeType = "nvarchar" | col. nativeType = "varchar") fixedlength = "FixedLength = \" false \ ""; string precision = ""; if (col. scale> 0) precision = "Precision = \" "+ col. scale + "\" "; // col. precision %> <Property Name = "<% = Col. name %> "Type =" <% = type %> "<% = maxlength %> <% = fixedlength %> <% = unicode %> <% = Nullable %> <% = precision %> <Documentation> <Summary> <% = col. description %> </Summary> </Documentation> </Property> <% }%> </EntityType> <% }%> <! -- Manual modify end --> </Schema>
// Generate the file to hard
<% -- Name: GenerateFiles. cstAuthor: pukuiminDescription: generate and output the xml file Template -- %> <% @ Template Language = "C #" TargetLanguage = "Text" Inherits = "CodeTemplate" Encoding = "UTF-8" %> <% @ assembly Name = "SchemaExplorer" %> <% @ Import Namespace = "SchemaExplorer" %> <% -- database -- %> <% @ Property Name = "SourceDatabase" Type = "SchemaExplorer. databaseSchema "DeepLoad =" True "Optional =" False "Category =" required Database "Description =" Database "%> <% -- The Table to be printed -- %> <% @ Property Name = "Table" Type = "SchemaExplorer. tableSchema "DeepLoad =" True "Optional =" False "Category =" Required data table "Description =" table. "%> <% @ Property Name =" IsAll "Type =" System. boolean "Optional =" False "Category =" generate all tables "Default =" True "Description =" generate all tables "%> <% -- register an entity layer Model template -- %> <% @ Register Name = "edmx_sumary" Template = "edmx_sumary.cst" MergeProperties = "Flase" ExcludeProperties = "" %> <s Export runat = "template"> // solution output path private string Directory = String. empty; [Editor (typeof (System. windows. forms. design. folderNameEditor), typeof (System. drawing. design. UITypeEditor)] [Optional, NotChecked] [DefaultValue ("")] public string OutputDirectory {get {return Directory;} set {if (value. endsWith ("\") value = value. substring (0, value. length-1); Directory = value;} public string Get Subspace (string tableName) {for (int I = tableName. length-1; I> = 0; I --) {if (tableName [I]> = 'A' & tableName [I] <= 'Z ') {tableName = tableName. substring (0, I) + tableName. substring (I, 1 ). toLower () + tableName. substring (I + 1); break;} return tableName;} // generate Entity class private void GenerateEntityClasses () {CodeTemplate edmx_sumaryTemplate = new edmx_sumary (); /* /// generate the basic BaseDAL abstract class string basedalDirectory = Outpu TDirectory + "\ DAL \ BaseDAL. cs "; basedalTemplate. setProperty ("NM", MyNameSpace); // namespace basedalTemplate. renderToFile (basedalDirectory, true); // file output Debug. writeLine (basedalDirectory + "created successfully. "); */Response. writeLine ("........................ Start generating ........................ "); String edmx_sumaryDirectory = OutputDirectory +" \ edmx_sumary.xml "; // generate the Service file catalog (" SourceDatabase ", SourceDatabase); edmx_sumaryTemplate.SetProperty (" Table ", Table ); edmx_sumaryTemplate.SetProperty ("IsAll", IsAll); edmx_sumaryTemplate.RenderToFile (edmx_sumaryDirectory, true); // file output Response. writeLine (edmx_sumaryDirectory + "created successfully. "); Response. writeLine ("........................ Complete generation ........................ ") ;}</Script> <% // create Entity class this. GenerateEntityClasses (); Debug. WriteLine (" All generation is complete! "); Response. WriteLine (" All generation is complete! "); %>
C # create a console application and add a comment to the fields in edmx:
Class Program {static void Main (string [] args) {Work wk = new Work (); wk. doWork (); Console. readKey () ;}} public class Work {// put all files in the generated exe directory and execute public string InXmlPath = "edmx_sumary.xml "; // public string OutXmlPath = "SysEntities. edmx "; public void DoWork () {string InFile = Path. combine (AppDomain. currentDomain. baseDirectory, InXmlPath); string OutFile = Path. combine (AppDomain. CurrentDomain. baseDirectory, OutXmlPath); XmlDocument inputDoc = new XmlDocument (); XmlDocument outputDoc = new XmlDocument (); inputDoc. load (InFile); outputDoc. load (OutFile); XmlNodeList inList = inputDoc. selectNodes ("/Schema/EntityType"); if (inList = null | inList. count = 0) return; XmlNamespaceManager xmlnsManager = new XmlNamespaceManager (outputDoc. nameTable); xmlnsManager. addNamespace ("edm X "," http://schemas.microsoft.com/ado/2009/11/edmx "); xmlnsManager. addNamespace ("annotation", "http://schemas.microsoft.com/ado/2009/02/edm/annotation"); xmlnsManager. addNamespace ("customannotation", "http://schemas.microsoft.com/ado/2009/02/edm/customannotation"); // "/edmx: Edmx/edmx: Runtime/edmx: ConceptualModels/edmx: Schema/edmx: EntityType" XmlNode outSchemaNode = outputDoc. selectSingleNode ("/ed Mx: Edmx/edmx: Runtime/edmx: ConceptualModels ", xmlnsManager ). childNodes [0]; foreach (XmlNode inNode in inList) // recyclically comments to multiple tables {if (inNode. nodeType = XmlNodeType. comment) continue; XmlElement inElement = (XmlElement) inNode; string tableName = inElement. getAttribute ("Name"); Console. write (tableName + ";"); Console. writeLine (); XmlElement outElement = null; foreach (XmlNode outNodetemp in outSchemaNode. C HildNodes) // find the output file table node {if (outNodetemp. nodeType = XmlNodeType. comment | outNodetemp. name = "EntityContainer" | outNodetemp. name = "Association") continue; XmlElement xe_edmx = (XmlElement) outNodetemp; string o_tableName = xe_edmx.GetAttribute ("Name"); if (o_tableName = tableName) {outElement = xe_edmx; break ;}} if (outElement = null) continue; Console. writeLine ("edmx:" + out Element. getAttribute ("Name"); foreach (XmlNode field in outElement. childNodes) {if (field. nodeType = XmlNodeType. comment) continue; XmlElement field_element = (XmlElement) field; // if (field_element.NodeType = XmlNodeType. comment | field_element.Name = "NavigationProperty" | field_element.Name = "Key") continue; if (field_element.Name = "Documentation") outElement. removeChild (field); // Delete the output File Original table comment} // outputDoc. save (OutFile + "new. edmx "); return; foreach (XmlNode field in inElement. childNodes) // the fields in the input loop file {if (field. nodeType = XmlNodeType. comment | field. name = "Key") continue; XmlElement field_element = (XmlElement) field; string fieldName = field_element.GetAttribute ("Name"); if (field_element.Name = "Documentation ") {XmlElement temp = outputDoc. createElement ("Documentation "," Http://schemas.microsoft.com/ado/2013/11/edm/Property "); XmlElement tempc = outputDoc. createElement ("Summary"); tempc. innerText = field. innerText; temp. appendChild (tempc); Console. writeLine ("val:" + temp. value); outElement. appendChild (temp);} else if (field_element.Name = "Property") {foreach (XmlNode outfield in outElement. childNodes) {if (outfield. nodeType = XmlNodeType. comment) continue; X MlElement outfield_element = (XmlElement) outfield; if (outfield_element.GetAttribute ("Name") = fieldName) {outfield. innerXml = field. innerXml; // modify the attribute of the input file to break; }}}} outputDoc in the output file. save (OutFile + "new. edmx "); // save it to a new file or to the original file. It is best to back up the original file first. }}
The tt template also needs to write code to read comments from edmx. Now the comments are already in the edmx file. You can use NotePad to open the comments.
There are a lot of online code for reading comments from edmx by tt.