从PowerDesigner表字段的Name到EF实体类属性的Display Name(根据PowerDesigner生成EF实体类中文注释和验证元数据)
2016-10-18 11:37
671 查看
第一步:将PowerDesigner表字段的中文Name填入Comment中:工具-Execute Commands-Edit/Run Script...
Model1.tt Property段
效果:
参考:
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
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段
效果:
//------------------------------------------------------------------------------ // <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
相关文章推荐
- Powerdesigner16.5将物理模型表中的name在创建数据库时生成对应字段的注释
- 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能
- PowerDesigner 中 根据对象的name和comment 生成注释的办法
- PowerDesigner根据NAME自动生成注释
- C#集合篇,在业务背景下(***产品升级管理):依赖注入,变量声明,三元表达式,常用字符串相关操作方法,ADO.NET,EF机制,T4模板自动生成实体类,ref变量巧用,属性实际运用,唯一性验证
- PowerDesigner 12.5 开启注释列 将Comment(注释)及Name(名称)内容互相COPY的VBS代码 根据名称生成注释(完整示例)
- powerdesigner 生成脚本时 名字总是中文的解决办法 属性是name 不是code
- Powerdesigner16.5将物理模型表中的name在创建数据库时生成对应字段的注释
- PowerDesigner 12.5 开启注释列 将Comment(注释)及Name(名称)内容互相COPY的VBS代码 根据名称生成注释(完整示例)
- PowerDesigner中翻转生成PDM图时把Name属性变成注释(转)
- 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能
- 【摘要】PowerDesigner 根据NAME属性自动生成表和列注释
- 从数据库生成PD 并且设置数据库字段说明对应PD的NAME属性
- 将PowerDesigner中列的name生成到注释comment中去
- SQL Server 根据表名创建实体类的字段和属性
- 反射+注释,根据实体类对象生成SQL语句工具类
- SQL Server 根据表名创建实体类的字段和属性
- PowerDesigner中生成SQL SERVER2005字段注释的解决方法
- 根据表名创建实体类的字段和属性
- 在Powerdesigner中,根据已有字段的Name值替换Code相同的Name的值