真爱无限的知识驿站

学习积累技术经验,提升自身能力

给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="System-CSharpAlias" Description="数据库字段类型与C#类型的映射表" %>
<%-- 数据库 --%>
<%@ 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="是否生成全部表" %>
<% 
/*
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;
    }
}
*/
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>();//存储外键列及对应的主键表名
foreach(var item in Table.ForeignKeys)
{
    fkeys.Add(item.ForeignKeyMemberColumns[0].Name);
    //Response.Write(item.ForeignKeyMemberColumns[0].Name+"--"+item.PrimaryKey.Table.Name+"
"); 
}
TableSchemaCollection  tables=new TableSchemaCollection();;
if(IsAll !=true)
{  
   tables.Add(Table); 
}
else
{
   tables=this.SourceDatabase.Tables; 
}
     
%>
<% 
//if(tables==null){Response.WriteLine("空");return;} 
//else {Response.WriteLine(tables.Count);} 
%>
<Schema>
<!--manual modify begin-->
        <!-- 复制替换 edmx文件中的 edmx:ConceptualModels 下的 Schema 内、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 lowertype=CSharpAlias[col.SystemType.FullName];
                 string type=col.SystemType.FullName.Replace("System.","");
                 string Nullable=col.AllowDBNull?"":" Nullable="false"";//是否可空
                 string maxlength=(type=="String"?" MaxLength=""+col.Size+""":"");//字符串最大长度
                 string unicode=(col.DataType.ToString()=="AnsiString"?" Unicode="false"":(col.DataType.ToString()=="String"?" Unicode="true"":""));//是否可存储中文
                 string fixedlength="";//长度是否固定
                 if(col.NativeType=="nchar" || col.NativeType=="char") fixedlength=" FixedLength="true""; //col.NativeType为数据库中的类型
                 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>


//生成文件到硬盘上

<%-- 
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 = value.Substring(0, value.Length -1);
            Directory = value;
        } 
    }
    public string GetSubspace(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;
    }
        //生成实体Entity类
    private void GenerateEntityClasses()
    {
        CodeTemplate edmx_sumaryTemplate =new edmx_sumary();
        /*
        ////生成基础BaseDAL抽象类
        string basedalDirectory = OutputDirectory +"\DAL\BaseDAL.cs";
        basedalTemplate.SetProperty("NM",MyNameSpace);//命名空间
        basedalTemplate.RenderToFile(basedalDirectory,true);//文件输出
        Debug.WriteLine(basedalDirectory +" 创建成功.");
        */
        
            Response.WriteLine("……………………开始生成……………………");
            string edmx_sumaryDirectory = OutputDirectory +"\edmx_sumary.xml";
            
            //生成Service文件
            edmx_sumaryTemplate.SetProperty("SourceDatabase",SourceDatabase);
            edmx_sumaryTemplate.SetProperty("Table",Table);
            edmx_sumaryTemplate.SetProperty("IsAll",IsAll);
            edmx_sumaryTemplate.RenderToFile(edmx_sumaryDirectory,true);//文件输出
            Response.WriteLine(edmx_sumaryDirectory +" 创建成功.");
            
            Response.WriteLine("…………………… 完成生成……………………");
    }
</script>
<%
    //创建实体层Entity类
    this.GenerateEntityClasses();
  &nb

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号