从PowerDesigner表字段的Name到EF实体类属性的Display Name(根据PowerDesigner生成EF实体类中文注释和验证元数据)
第一步:将PowerDesigner表字段的中文Name填入Comment中:工具-Execute Commands-Edit/Run Script...
'****************************************************************************** '* 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
(可保存为文件以后直接打开执行)
第二步:从PowerDesigner表字段的Comment到SQL Server表字段的MS_Description(说明):数据库-Generate Database...
第三步:创建EF edmx文件
第四步:将SQL Server表字段的MS_Description(说明)添加到EF edmx文件:
EFTSQLDocumentation.Generator.exe -c "Data Source=.;Initial Catalog=xxxdb;User ID=sa;Password=yyy;" -i "上一步生成的edmx文件的完整路径"
然后刷新edmx(从数据库更新模型),可以多刷两遍。
(EFTSQLDocumentation.Generator.exe可到https://eftsqldocgenerator.codeplex.com/下载)
最后一步:修改生成实体类的T4模板(默认叫Model1.tt):
Model1.tt Namespace段:
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, " /// <summary> /// {0} - {1} /// </summary> ", edmProperty.Documentation.Summary ?? "", edmProperty.Documentation.LongDescription ?? ""); doc += string.Format( CultureInfo.InvariantCulture, "[Display(Name = "{0}")] ", edmProperty.Documentation.Summary.Replace('(', '_').Replace(')', '_').Replace('(', '_').Replace(')', '_').Replace(" ", "") ?? "", edmProperty.Documentation.LongDescription ?? ""); } if (!edmProperty.Nullable) { doc += "[Required(ErrorMessage = "您需要填写{0}!")] "; } var maxLengthFacet = (Facet)edmProperty.TypeUsage.Facets.SingleOrDefault(f => f.Name == "MaxLength"); if(maxLengthFacet != null && !maxLengthFacet.IsUnbounded) { doc += "[StringLength("+ maxLengthFacet.Value +", ErrorMessage = "{0}长度不能超过"+ maxLengthFacet.Value +"")] "; } 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, " /// <summary> /// {0} - {1} /// </summary> ", 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))); }