從PowerDesigner表字段的Name到EF實體類屬性的Display Name(根據PowerDesigner生成EF實體類中文註釋和驗證元數據)

第一步:將PowerDesigner表字段的中文Name填入Comment中:工具-Execute Commands-Edit/Run Script...html

'******************************************************************************
'* File:     name2comment.vbs
'* Title:    Name to Comment Conversion
'* Model:    Physical Data Model
'* Objects: Table, Column, View
'* Author:   steveguoshao
'* Created: 2013-11-29
'* Mod By:   
'* Modified: 
'* Version: 1.0
'* Memo:     Modify from name2code.vbs
'******************************************************************************

Option   Explicit 
ValidationMode   =   True 
InteractiveMode   =   im_Batch

Dim   mdl   '   the   current   model

'   get   the   current   active   model 
Set   mdl   =   ActiveModel 
If   (mdl   Is   Nothing)   Then 
  MsgBox   "There   is   no   current   Model " 
ElseIf   Not   mdl.IsKindOf(PdPDM.cls_Model)   Then 
  MsgBox   "The   current   model   is   not   an   Physical   Data   model. " 
Else 
  ProcessFolder   mdl 
End   If

'   This   routine   copy   name   into   comment   for   each   table,   each   column   and   each   view 
'   of   the   current   folder 
Private   sub   ProcessFolder(folder) 
  Dim   Tab   'running     table 
  for   each   Tab   in   folder.tables 
    if   not   tab.isShortcut   then 
      tab.comment   =   tab.name 
      Dim   col   '   running   column 
      for   each   col   in   tab.columns 
        col.comment=   col.name 
      next 
    end   if 
  next

  Dim   view   'running   view 
  for   each   view   in   folder.Views 
    if   not   view.isShortcut   then 
      view.comment   =   view.name 
    end   if 
  next

  '   go   into   the   sub-packages 
  Dim   f   '   running   folder 
  For   Each   f   In   folder.Packages 
    if   not   f.IsShortcut   then 
      ProcessFolder   f 
    end   if 
  Next 
end   sub
name2comment.vbs

(可保存爲文件之後直接打開執行)sql

 

第二步:從PowerDesigner表字段的Comment到SQL Server表字段的MS_Description(說明):數據庫-Generate Database...數據庫

 

第三步:建立EF edmx文件app

 

第四步:將SQL Server表字段的MS_Description(說明)添加到EF edmx文件:ide

EFTSQLDocumentation.Generator.exe -c "Data Source=.;Initial Catalog=xxxdb;User ID=sa;Password=yyy;" -i "上一步生成的edmx文件的完整路徑"

而後刷新edmx(從數據庫更新模型),能夠多刷兩遍。工具

(EFTSQLDocumentation.Generator.exe可到https://eftsqldocgenerator.codeplex.com/下載)ui

 

最後一步:修改生成實體類的T4模板(默認叫Model1.tt):spa

Model1.tt Namespace段:code

public void BeginNamespace(CodeGenerationTools code)
{
    var codeNamespace = code.VsNamespaceSuggestion();
    if (!String.IsNullOrEmpty(codeNamespace))
    {
#>
namespace Models
{
    using System.ComponentModel.DataAnnotations;
<#+
        PushIndent("    ");
    }
}

public void EndNamespace(CodeGenerationTools code)
{
    if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion()))
    {
        PopIndent();
#>
}
<#+
    }
}

 

    public string Property(EdmProperty edmProperty)
    {
        string doc = "";
        if (edmProperty.Documentation != null)
        {
            doc = string.Format(
            CultureInfo.InvariantCulture,
            "\n\t\t/// <summary>\n\t\t/// {0} - {1}\n\t\t/// </summary>\n\t\t",
            edmProperty.Documentation.Summary ?? "",
            edmProperty.Documentation.LongDescription ?? "");

            doc += string.Format(
            CultureInfo.InvariantCulture,
            "[Display(Name = \"{0}\")]\n\t\t",
            edmProperty.Documentation.Summary.Replace('(', '_').Replace(')', '_').Replace('', '_').Replace('', '_').Replace(" ", "") ?? "",
            edmProperty.Documentation.LongDescription ?? "");
        }

        if (!edmProperty.Nullable)
        {
            doc += "[Required(ErrorMessage = \"您須要填寫{0}!\")]\n\t\t";
        }

        var maxLengthFacet = (Facet)edmProperty.TypeUsage.Facets.SingleOrDefault(f => f.Name == "MaxLength");
        if(maxLengthFacet != null && !maxLengthFacet.IsUnbounded)
        {
            doc += "[StringLength("+ maxLengthFacet.Value +", ErrorMessage = \"{0}長度不能超過"+ maxLengthFacet.Value +"\")]\n\t\t";
        }

        return doc + string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            Accessibility.ForProperty(edmProperty),
            _typeMapper.GetTypeName(edmProperty.TypeUsage),
            _code.Escape(edmProperty),
            _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    }

    public string NavigationProperty(NavigationProperty navigationProperty)
    {
        var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
        string doc = "";
        if (navigationProperty.Documentation != null)
        {
            doc = string.Format(
            CultureInfo.InvariantCulture,
            "\n\t\t/// <summary>\n\t\t/// {0} - {1}\n\t\t/// </summary>\n\t\t",
            navigationProperty.Documentation.Summary ?? "",
            navigationProperty.Documentation.LongDescription ?? "");
        }

        return doc + string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)),
            navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
            _code.Escape(navigationProperty),
            _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
    }
Model1.tt Property段

 

 

效果:orm

//------------------------------------------------------------------------------
// <auto-generated>
//    此代碼是根據模板生成的。
//
//    手動更改此文件可能會致使應用程序中發生異常行爲。
//    若是從新生成代碼,則將覆蓋對此文件的手動更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace Models
{
    using System.ComponentModel.DataAnnotations;
    using System;
    using System.Collections.Generic;
    
    public partial class Custom
    {
        
        /// <summary>
        /// 客戶ID - 
        /// </summary>
        [Display(Name = "客戶ID")]
        [Required(ErrorMessage = "您須要填寫{0}!")]
        public int CustomID { get; set; }
        
        /// <summary>
        /// 客戶名稱 - 
        /// </summary>
        [Display(Name = "客戶名稱")]
        [Required(ErrorMessage = "您須要填寫{0}!")]
        [StringLength(60, ErrorMessage = "{0}長度不能超過60")]
        public string CustomName { get; set; }
        

 

 

參考:

http://www.iteye.com/topic/1138201

http://www.cnblogs.com/hhhh2010/p/5344256.html

http://stackoverflow.com/questions/13931159/add-documentation-to-generated-code-in-entity-framework-model-first

https://eftsqldocgenerator.codeplex.com/

Entity Framework Power Tools:https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/

 

謝謝小夥伴wwl、jxt

相關文章
相關標籤/搜索