项目中需要使用支持PostgreSQL的ORM代码生成工具,搜寻了几个常用工具进行了考察,考察的原则是“支持PostgreSQL、易于使用”,最终选择了EntitysCodeGenerate。
考察结果如下:
动软代码生成器
- 下载地址:http://www.maticsoft.com/download.aspx
- 使用版本:2.78(2013-3-12)
- 支持数据库:SQL Server,Oracle,MySQL,OleDb,SQLite
- 参考资料:动软代码生成器帮助中心
- 存在问题:不支持PostgreSQL
MyGeneration
- 下载地址:http://sourceforge.net/projects/mygeneration/files/
- 使用版本:1.3.1(2011-1-2) && 1.3.0.9(2008-10-6)
- 支持数据库:SQL Server,Oracle,IBM DB2,Microsoft Access,MySQL,PostgreSQL,Firebird,Interbase,SQLite,VistaDB,Advantage,IBM iSeries,CSV & other delimited text files
- 缺点:只支持单表,复杂的查询还要先写视图。
- 参考资料:
- 存在问题:无法使用(操作系统为 Win7 64 位)
- 安装
mygeneration_131_20110102.exe
,成功配置,连接数据库成功,但是无法生成代码。 - 安装
mygeneration_1309_20081006.exe
,配置时驱动列表为空,无法使用。 - 安装过程的问题处理:
You cannot run MyGeneration without having MDAC 2.7+ installed. It is not included in the installer because the file is large and most people already have it installed.
- 处理方法:安装时去掉
Detect MDAC 2.7+
选项 - 参考:win7 64 下安装MyGeneration 遇到的问题解决方法
- 处理方法:安装时去掉
- 驱动列表为空
- 处理方法: 修改RegisterMyMeta.bat文件,将框架v2.0.50727修改为v4.0.30319
- 参考:MyGeneration 1.3 配置界面driver下拉列表为空的解决办法
- 处理结果:无效
- 安装
Keelkit
- 下载地址:http://keelkit.codeplex.com/releases/view/88378
- 使用版本:3.0.7600.638(2012-5-25)
- 支持数据库:Microsoft Access,SQL Server,MySql,Oracle
- 参考资料:KeelKit系列文章(二):快速配置(相关资料极少)
- 存在问题:不支持PostgreSQL
- 其他问题:不支持 VS2013
Gentle.NET
- 下载地址:http://sourceforge.net/projects/gopf/?source=typ_redirect
- 使用版本:1.5.0(2013-4-3)
- 支持数据库:Firebird,Jet,MySQL,PostgreSQL,Oracle,OracleODP,SQLite,SQL Server
- 参考资料:
- Gentle.NET初探
- 其中用到了CodeSmith生成代码
- 下载地址:CodeSmith 7,最新绿色破解版,带注册机
- 破解方法:CodeSmith 7.01破解下载
- 其中用到了CodeSmith生成代码
- GENTLE.NET快速上手
- Gentle.NET初探
- 存在问题:在编辑
GentleBusinessObject.cst
文件的SourceTable参数时,无法指定PostgreSQL数据源(可能是需要自己按照GentleBusinessObject.cst
文件编写对应的配置。 - 其他问题:Gentle.NET的使用方式是,先利用CodeSmith等生成最基本的Model(无增删查改的方法),再使用
Gentle.Framework.Broker
对Model进行增删查改。配置上比较复杂,CodeSmith一次只能生成一个数据表,用起来不太方便。
EntitysCodeGenerate
- 下载地址:http://www.downxia.com/downinfo/26449.html#softdown
- 使用版本:4.6(2014-5-5)
- 支持数据库:Oracle,SQL Server,Access,MySQL,Sybase,SQLite,DB2,OleDb,PostgreSQL,DM(达梦),PowerDesigner
- 参考资料:ORM框架-VB/C#.Net实体代码生成工具(EntitysCodeGenerate)【ECG 4.2】的相关使用
- 测试结果:成功,生成的代码示例如下:
/******************************************
* 模块名称:实体 role
* 当前版本:1.0
* 开发人员:Administrator
* 生成时间:2015/7/8
* 版本历史:此代码由 VB/C#.Net实体代码生成工具(EntitysCodeGenerate 4.6) 自动生成。
*
******************************************/
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Xml;
using System.IO;
using System.Text;
using System.Database;
using System.Database.ORMap;
using System.Database.Extend;
namespace ZHD.GIS.Entity
{
/// <summary>
/// 实体 role
/// </summary>
[Description("Primary:id")]
[Serializable]
public partial class role : BaseEntity
{
#region 构造函数
/// <summary>
/// 实体 role
/// </summary>
public role(){}
/// <summary>
/// 实体 role
/// </summary>
/// <param name="keyValue">主键id对应的值</param>
public role(object keyValue)
{
ListDictionary list = new ListDictionary();
list.Add("id", keyValue);
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this, this);
ormap.GetEntity(list);
}
/// <summary>
/// 实体 role
/// </summary>
/// <param name="colName">实体字段名</param>
/// <param name="colValue">实体字段名(colName)匹配相等的值</param>
public role(string colName, object colValue)
{
ListDictionary list = new ListDictionary();
list.Add(colName, colValue);
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this, this);
ormap.GetEntity(list);
}
/// <summary>
/// 实体 role
/// </summary>
/// <param name="colNames">实体字段名数组</param>
/// <param name="colValues">实体字段名数组(colNames)匹配相等的值</param>
public role(string[] colNames, object[] colValues)
{
if (colNames.Length == colValues.Length)
{
ListDictionary list = new ListDictionary();
for (int i = 0; i < colNames.Length; i++)
{
list.Add(colNames[i], colValues[i]);
}
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this, this);
ormap.GetEntity(list);
}
}
/// <summary>
/// 实体 role
/// </summary>
/// <param name="dbCore">数据库访问核心类DbCore实例(用于DbCore事务)</param>
/// <param name="colName">实体字段名</param>
/// <param name="colValue">实体字段名(colName)匹配相等的值</param>
public role(DbCore dbCore, string colName, object colValue)
{
ListDictionary list = new ListDictionary();
list.Add(colName, colValue);
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this, this);
ormap.GetEntity(dbCore, list);
}
/// <summary>
/// 实体 role
/// </summary>
/// <param name="dbCore">数据库访问核心类DbCore实例(用于DbCore事务)</param>
/// <param name="colNames">实体字段名数组</param>
/// <param name="colValues">实体字段名数组(colNames)匹配相等的值</param>
public role(DbCore dbCore, string[] colNames, object[] colValues)
{
if (colNames.Length == colValues.Length)
{
ListDictionary list = new ListDictionary();
for (int i = 0; i < colNames.Length; i++)
{
list.Add(colNames[i], colValues[i]);
}
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this, this);
ormap.GetEntity(dbCore, list);
}
}
#endregion
#region 私有变量
private string _id = null;
private string _name = null;
private string _comment = null;
#endregion
#region 公共属性
/// <summary>
/// 主键 id(NOT NULL)
/// </summary>
[DataObjectField(true)]
public string id
{
set{ _id=value;}
get{
return _id;}
}
/// <summary>
/// name(NOT NULL)
/// </summary>
[DataObjectField(false)]
public string name
{
set{ _name=value;}
get{
return _name;}
}
/// <summary>
/// comment
/// </summary>
[DataObjectField(false)]
public string comment
{
set{ _comment=value;}
get{
return _comment;}
}
#endregion
#region 常用 增、删、改、查 操作(注:.Net下数值型字段初始值默认为0;带?的数值型字段值默认为null.)
#region public override int DelInsert/DelInsertEx 扩展方法
/// <summary>
/// 先Delete后Insert 比较实体前后默认的初始值,且不插入与实体初始值相同的字段值
/// </summary>
/// <returns>先Delete后Insert并返回受影响的行数</returns>
public override int DelInsert()
{
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this);
return ormap.DelInsert(new role());
}
/// <summary>
/// 先Delete后Insert 插入所有字段,其中若与实体字段初始值相同则置该字段为空
/// </summary>
/// <returns>先Delete后Insert并返回受影响的行数</returns>
public override int DelInsertEx()
{
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this);
return ormap.DelInsertEx(new role());
}
#endregion
#region public override int Insert 增加
/// <summary>
/// 通过实体映射插入表中一条数据,插入与初始值不同的字段
/// </summary>
/// <returns>执行插入数据并返回受影响的行数</returns>
public override int Insert()
{
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this);
return ormap.Insert(new role());
}
#endregion
#region public override int Update 更新
/// <summary>
/// 通过实体映射及主键约束更新表中一条数据,并与比较实体初始值比较,若内容不同则更新之,否则不更新(并与实体默认初始值比较确定更新哪些字段,与默认初始值一样的字段将不更新)
/// </summary>
/// <returns>执行更新并返回受影响的行数</returns>
public override int Update()
{
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this);
return ormap.Update(new role());
}
#endregion
#region public override int Save 保存 规则:按主键判断有就更新,没有就插入,且不保存与实体初始值相同的字段值
/// <summary>
/// 保存 比较实体前后的值,若有与主键记录相同的就更新,没有就插入,且不保存与实体初始值相同的字段值
/// </summary>
/// <returns>执行更新或插入数据操作并返回受影响的行数</returns>
public override int Save()
{
ORMap<BaseEntity> ormap = new ORMap<BaseEntity>(this);
return ormap.Save1(new role());
}
#