从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
name2comment.vbs
(可保存为文件以后直接打开执行)
第二步:从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,
"\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(, 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
从PowerDesigner表字段的Name到EF实体类属性的Display Name(根据PowerDesigner生成EF实体类中文注释和验证元数据)的更多相关文章
- pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等
pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等 pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等 pojo类对应的就是数据库中的表 ...
- PowerDesigner表创建脚本双引号问题
在使用PowerDesigner表属性的Preview查看创建脚本的时候,发现大多表名和字段名都加上了双引号,而且有引号的都是大小写混合的,导致创建的表里,表名和字段名也都是大小写混合的. 在一番搜索 ...
- 【转】PowerDesigner物理数据表生成C#实体类Model
model实体类是什么: 在三层架构UI,BLL,DAL中,有时用户插入一条记录到数据库中,必然会有不少数据,按正常编程,也必然会一下子调用某个函数传入不少参数.为了减少参数的数量,达到高效简洁的效果 ...
- salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...
- EF Power Tools的Reverse Engineer Code First逆向生成Model时处理计算字段
VS2013上使用EF Power Tools的Reverse Engineer Code First逆向生成Model时,没有处理计算字段.在保存实体时会出现错误. 可以通过修改Mapping.tt ...
- 知识备忘phpcms 简单解析一 数据表字段
PHPCMS V9帮助中心 数据结构 phpcms v9 数据... phpcms v9 数据... PHPSSO 数据库结... phpcms v9 数据表结构 在线版 PHPCMS V9 数据结构 ...
- plsql修改表字段alter
场景:在生产过程中有时候需要不同的环境中修改表字段,使用sql语句比较方便! 1 演示 --添加字段的语法 alter table tablename add (column datatype [de ...
- TSQL:A表字段与B表中的关联,关联条件中一列是随机关联的实现方式
A表字段与B表中的关联,关联条件中一列是随机关联的实现方式 create table test( rsrp string, rsrq string, tkey string, distan strin ...
- mysql 动态拼接表字段,值 mybatis 动态获取表字段
-- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...
随机推荐
- iOS之自定义控件
一.使用纯代码方式 initWithFrame:中添加子控件 layoutSubViews中设置子控件的fame 对外设置数据接口,重写setter方法给子控件设置数据显示 在的viewControl ...
- 在db2数据库上模拟死锁场景 还是z上的
如果条件允许,起两个线程互相抢资源就行了,但问题是,时间上还需要同步,要做到完美控制,还得加其他逻辑,忒费事,所以可以用下面的办法: 在目标表上直接加个锁……简单,粗暴,直接……很好…… LOCK T ...
- [leetcode] 题型整理之排序
75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...
- STM32F051芯片解密STM32F091IC解密STM32F042单片机破解多少钱?
STM32F051芯片解密STM32F091IC解密STM32F042单片机破解多少钱? STM32F0系列解密的芯片: STM32F031解密 | STM32F051解密 | STM32F091解密 ...
- BZOJ 1455 罗马游戏 ——左偏树
[题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...
- jquery点击元素之外触发事件
$("#errorMsg_layer").bind("click",function(e){ if($(e.target).closest("#err ...
- 学习js 优先级
以前很少关注js优先级 主要哦是技术菜鸟老加班没时间技术菜鸟 最重要的是记不住特点.......... 1级 . [] () 字段访问.数组索引.函数调用和表达式分组 通过观察可以发现 . 字段访问- ...
- 基于华清远见STM32f051的 IIC从模式实现方法
作者:卢老师,华清远见嵌入式学院讲师. 在大多情况下,我们使用MCU控制传感器,节点以及相关从设备,但在较为复杂的系统中,有时候也会使用MCU做为从设备. 下面是关于stm32f051的从模式实现方法 ...
- Linux下定时执行任务的几种方式
如果说我说如果,你的某一个目录下会经常的生成一些垃圾文件,比如访问日志.错误日志.core文件,而你又不想过几分钟就去手动检查一下,那么可以使用定时执行任务的方式来解决.目前我所知道的可以执行定时任务 ...
- react-native 之布局篇
一.宽度单位和像素密度 react的宽度不支持百分比,设置宽度时不需要带单位,那么默认的单位是什么呢? /** * Sample React Native App * https://github.c ...