用Powerdesigner生成NHebernate的MAP文件
昨天通过修改Powerdesigner的配置文件实现特殊的C#代码生成要求后,今天突然想到:如果能够在Powerdesigner中直接生成Map文件,那就比较爽了。 
下午试了一会,发现完全可行,其具体方式是
在Profile\Classifier\Generated Files下添加一项,如hbm.xml,该项设置为
文件名:%topContainerCode%.hbm.xml
说明 :
.if (%isGenerated%)
.set_object(_usingContext,,new)
[\ %NHebernateHead%\n]\
%NHebernateBody% \n
%NHebernateFoot%
.endif
设置变量的值,如将 %NHebernateBody%设置为:
<class name="%MyClassPathName%.%Parent.path%, %MyClassPathName%" table="%Parent.path%"> \n
.foreach_item(Attributes)
.if(%code%==%myPrimaryKey%)
<id name="%code%" column="%code%" type="String">
<generator class="assigned" />
</id> \n
.endif
.if (%dataType%=="string")
<property name="%code%" column="%code%" type="String" /> \n
.endif
.if (%dataType%=="int")
<property name="%code%" column="%code%" type="Int32" /> \n
.endif
.next
</class>
(数据类型的转换部分可以自己加上)
设置完成,在Project文件中添加对这些文件的引用。
将Profile\BasePackage\Templates\VisualStudio.NET\ProjectSourceFiles设置为:
.// Declare classifiers of the package
.foreach_item(Classifiers,,,%IsShortcut%==false)
.if (%IsSelected%) and (%isInner% == false)
<File
RelPath = "%sourceFilePath%"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "%Parent.path%\\%topContainerCode%.hbm.xml"
BuildAction = "EmbeddedResource"
/>
.endif(\n)
.next
.// Declare classifiers of the subpackages
.foreach_item(Packages,,,%IsShortcut%==false)
.if (%isAssembly% == false)
%ProjectSourceFiles%
.endif
.next
搞定!

下午试了一会,发现完全可行,其具体方式是
在Profile\Classifier\Generated Files下添加一项,如hbm.xml,该项设置为
文件名:%topContainerCode%.hbm.xml
说明 :
.if (%isGenerated%)
.set_object(_usingContext,,new)
[\ %NHebernateHead%\n]\
%NHebernateBody% \n
%NHebernateFoot%
.endif
设置变量的值,如将 %NHebernateBody%设置为:
<class name="%MyClassPathName%.%Parent.path%, %MyClassPathName%" table="%Parent.path%"> \n
.foreach_item(Attributes)
.if(%code%==%myPrimaryKey%)
<id name="%code%" column="%code%" type="String">
<generator class="assigned" />
</id> \n
.endif
.if (%dataType%=="string")
<property name="%code%" column="%code%" type="String" /> \n
.endif
.if (%dataType%=="int")
<property name="%code%" column="%code%" type="Int32" /> \n
.endif
.next
</class>
(数据类型的转换部分可以自己加上)
设置完成,在Project文件中添加对这些文件的引用。
将Profile\BasePackage\Templates\VisualStudio.NET\ProjectSourceFiles设置为:
.// Declare classifiers of the package
.foreach_item(Classifiers,,,%IsShortcut%==false)
.if (%IsSelected%) and (%isInner% == false)
<File
RelPath = "%sourceFilePath%"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "%Parent.path%\\%topContainerCode%.hbm.xml"
BuildAction = "EmbeddedResource"
/>
.endif(\n)
.next
.// Declare classifiers of the subpackages
.foreach_item(Packages,,,%IsShortcut%==false)
.if (%isAssembly% == false)
%ProjectSourceFiles%
.endif
.next
搞定!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通