使用 EntitysCodeGenerate 生成 PostgreSQL 的代码

来源:互联网 发布:mac os 系统清理 编辑:程序博客网 时间:2024/05/14 19:17

下载地址:http://www.downxia.com/downinfo/26449.html#softdown

使用版本:4.6(2014-5-5)


代码生成:

  1. 下载安装,启动

    启动

  2. 选择“数据库类型”为 PostgreSQL,修改“数据库连接字符串”

    选择数据类型,修改数据库连接字符串

  3. 编辑“数据类型映射文件”(可选)(默认映射文件中的数据类型可能会不全,需要根据需要进行补充)

    • 可以先直接生成,在生成目录下的“相关配置”文件夹下查看“没有配置的数据类型映射信息.txt”以确定缺少的数据类型

    查看缺少的数据类型

    • 打开安装目录下的DbTypeToCSharpType.xml文件,找到<POSTGRESQL>部分,编辑需要的数据类型。例如:
      <bytea>byte</bytea><character_x0020_varying>string</character_x0020_varying><double_x0020_precision>double</double_x0020_precision><!-- timestamp with time zone --><timestamp_x0020_with_x0020_time_x0020_zone>DateTime</timestamp_x0020_with_x0020_time_x0020_zone><uuid>Guid</uuid><!-- bytea[] --><bytea_x005B__x005D_>byte[]</bytea_x005B__x005D_><!-- character(256) --><character_x0028_256_x0029_>string</character_x0028_256_x0029_>
  4. 选择“代码文件输出目录”(可选)

  5. 设置“代码命名空间”

  6. 选择“生成代码语言类型”(可选)

  7. 设置“作者”或“自定义代码头注释”

  8. 点击“生成代码”或“选择生成”(可选择数据表进行生成)


代码使用:

打开生成目录下的“相关配置\配置说明4.txt”,其中介绍了生成代码的使用方法

  1. 在解决方案中创建项目(例如:DAL),添加生成的实体代码和生成的实体基类BaseEntity.cs。

  2. 添加引用(“相关配置”文件夹下的dll)

    • System.Database.Provider.dll
    • System.Database.dll
    • System.Database.ORMap.dll
  3. 使用三层架构,添加BLL层,创建业务代码。

    假设生成的一个实体类为Test,其字段结构为:

    字段名称 字段类型 id uuid name character(50) comment character(256)

    则可添加如下的业务代码:

    using System;using DAL;namespace BLL{    public class TestBLL    {        // 添加一行记录        public Guid Add(string name, string comment)        {            Test test = new Test();            test.id = Guid.NewGuid();            test.name = name;            test.comment = comment;            test.Insert();            return test.id;        }        // 获取所有记录        public TestS GetAll()        {            return new TestS(true);        }        // 获取指定id的记录        public Test Get(Guid id)        {            Test test = new Test();            test.id = id;            return test.GetEntity();        }        // 更新指定id的记录        public void Update(Test test)        {            test.Update();        }        // 删除指定id的记录        public void Delete(Test test)        {            test.Delete();        }    }}
  4. 在UI层调用BLL实现数据库的操作

    TestBLL bll = new TestBLL();bll.Add("test1", ""); //插入记录Guid id = bll.Add("test2", ""); //插入记录TestS tests = bll.GetAll(); //获取全部记录Test test = bll.Get(id); //获取指定id的记录test.comment = "comment2"; bll.Update(test); //更新记录bll.Delete(test); //删除记录
0 0
原创粉丝点击