From the Name of the PowerDesigner table field to the Display Name of the EF object class attribute (EF object class Chinese annotation and verification metadata are generated based on PowerDesigner), powerdesigner object class

Source: Internet
Author: User

From the Name of the PowerDesigner table field to the Display Name of the EF object class attribute (EF object class Chinese annotation and verification metadata are generated based on PowerDesigner), powerdesigner object class

Step 1: Enter the Chinese Name of the PowerDesigner table field in Comment: tool-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: '* Modified:' * Version: 1.0 '* Memo: Modify from name2code. vbs '************************************** **************************************** option Explicit ValidationMode = True InteractiveMode = im_BatchDim 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 subName2comment. vbs

(It can be saved as a file and then directly opened for execution)

 

Step 2: from Comment of the PowerDesigner table field to MS_Description of the SQL Server table field (description): Database-Generate Database...

 

Step 3: Create an EF edmx File

 

Step 4: Add MS_Description (description) of the SQL Server table field to the EF edmx file:

EFTSQLDocumentation.Generator.exe-c "Data Source =.; Initial Catalog = xxxdb; User ID = sa; Password = yyy;"-I "full path of the edmx file generated in the previous step"

Then refresh edmx (update the model from the database). You can refresh it twice.

(EFTSQLDocumentation.Generator.exe can be downloaded at https://eftsqldocgenerator.codeplex.com)

 

Last step: Modify the T4 template (Model1.tt by default) for generating object classes ):

Model1.tt Namespace section:

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 // <summary> \ n \ t // {0}-{1} \ n \ t // </ summary> \ n \ t ", edmProperty. documentation. summary ?? "", EdmProperty. Documentation. LongDescription ?? ""); Doc + = string. format (CultureInfo. invariantCulture, "[Display (Name = \" {0} \ ")] \ n \ t", edmProperty. documentation. summary. replace ('(','_'). replace (')','_'). replace ('(','_'). replace (')','_'). replace ("","")?? "", EdmProperty. Documentation. LongDescription ?? "") ;}If (! EdmProperty. Nullable) {doc + = "[Required (ErrorMessage = \" you need to enter {0 }! \ ")] \ N \ t";} var maxLengthFacet = (Facet) edmProperty. typeUsage. facets. singleOrDefault (f => f. name = "MaxLength"); if (maxLengthFacet! = Null &&! MaxLengthFacet. isUnbounded) {doc + = "[StringLength (" + maxLengthFacet. value + ", ErrorMessage = \" {0} length cannot exceed "+ maxLengthFacet. value + "\")] \ n \ 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 (edm Property), _ 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 // <summary> \ n \ t // {0}-{1} \ n \ t // </ summary> \ n \ 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. why? ("ICollection <" + endType + ">"): endType, _ code. escape (navigationProperty), _ code. spaceAfter (Accessibility. forGetter (navigationProperty), _ code. spaceAfter (Accessibility. forSetter (navigationProperty )));}Model1.tt Property Section

 

 

Effect:

// -------------------------------------------------------------------------------- // <Auto-generated> // This code is generated based on the template. //// Manually changing this file may cause abnormal behavior in the application. // If the code is re-generated, the manual changes to the file will be overwritten. // </Auto-generated> // -------------------------------------------------------------------------------- namespace Models {using System. componentModel. dataAnnotations; using System. collections. generic; public partial class Custom {// <summary> // customer ID-// </summary> [Display (Name = "Customer ID")] [Required (ErrorMessage = "you need to enter {0 }! ")] Public int CustomID {get; set ;} /// <summary> /// customer Name-/// </summary> [Display (Name = "customer Name")] [Required (ErrorMessage = "you need to enter {0 }! ")] [StringLength (60, ErrorMessage =" {0} length cannot exceed 60 ")] public string CustomName {get; set ;}

 

 

Refer:

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/

 

Thank you, wwl and jxt.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.