首页 > 代码库 > 给ef6的实体模型 edmx文件加字段注释

给ef6的实体模型 edmx文件加字段注释

code smith 模版(要下载code smith软件使用):

//生成注释部分代码

<%-- 
Name:edmx文件加注释
Author: pukuimin
Description: 
--%>
<%@ Template Language="C#" TargetLanguage="XML" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Map Name="CSharpAlias" Src=http://www.mamicode.com/"System-CSharpAlias" Description="数据库字段类型与C#类型的映射表" %>>


//生成文件到硬上

<%-- 
Name:GenerateFiles.cst
Author: pukuimin
Description: 生成并输出xml文件的模版
--%>
<%@ Template Language="C#" TargetLanguage="Text" Inherits="CodeTemplate" Encoding="utf-8"%>
<%@ Assembly Name="SchemaExplorer"%>
<%@ Import Namespace="SchemaExplorer"%>

<%-- 数据库 --%>
<%@ Property Name="SourceDatabase"  Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="需要的数据库" Description="Database"%>
<%-- 要打印的表 --%>
<%@ Property Name="Table"  Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" Category="需要的数据表" Description="table ." %>
<%@ Property Name="IsAll"  Type="System.Boolean" Optional="False" Category="是否生成全部表" Default="True" Description="是否生成全部表" %>
<%-- 注册实体层Model模板 --%>
<%@ Register Name="edmx_sumary" Template="edmx_sumary.cst" MergeProperties="Flase" ExcludeProperties=""%>

<script runat="template">
    //解决方案输出路径
     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 = http://www.mamicode.com/value.Substring(0, value.Length -1);>


c#新建控制台应用程序,给edmx中的字段加注释:

    class Program
    {
        static void Main(string[] args)
        {
            Work wk = new Work();
            wk.DoWork();
            Console.ReadKey();
        }
    }
    public class Work
    {
//文件都放到生成的exe同目录下执行

        public string InXmlPath = "edmx_sumary.xml";//code smith生成的注释文件
        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("edmx", "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("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels", xmlnsManager).ChildNodes[0];
            foreach (XmlNode inNode in inList)//循环多张表注释
            {
                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.ChildNodes)//找出输入文件表节点 对应的输出文件 表节点
                {
                    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:" + outElement.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);//删除输出文件原有的表注释
                }
                //outputDoc.Save(OutFile + "new.edmx"); return;

                foreach (XmlNode field in inElement.ChildNodes)//循环输入文件各字段
                {
                    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;
                            XmlElement outfield_element = (XmlElement)outfield;
                            if (outfield_element.GetAttribute("Name") == fieldName)
                            {
                                outfield.InnerXml = field.InnerXml;//把输入文件的属性 修改到 输出文件里面
                                break;
                            }
                        }

                    }
                }
            }
            outputDoc.Save(OutFile + "new.edmx");//保存到新文件,也可以保存到原文件,最好先备份原文件。

        }

    }




tt模版中也要写代码从edmx中读取注释,现在注释已经在edmx文件中了,可以用记事本打开查看的。

关于tt读取edmx中的注释,网上代码很多 ,都可行。






给ef6的实体模型 edmx文件加字段注释