• ORM之PetaPoco


    近端时间从推酷app上了解到C#轻微型的ORM框架--PetaPoco。从github Dapper 开源项目可以看到PetaPoco排第四

    以下是网友根据官方介绍翻译,这里贴出来。

    PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。

    PetaPoco有以下特色:

    • 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中。
    • 工作于严格的没有装饰的Poco类,和几乎全部加了特性的Poco类
    • Insert/Delete/Update/Save and IsNew 等帮助方法。
    • 分页支持:自动得到总行数和数据
    • 支持简单的事务
    • 更好的支持参数替换,包括从对象属性中抓取命名的参数。
    • 很好的性能,剔除了Linq,并通过Dynamic方法快速的为属性赋值
    • T4模板自动生成Poco类
    • 查询语言是Sql……不支持别扭的fluent或Linq语法(仁者见仁,智者见智)
    • 包含一个低耦合的Sql Builder类,让内联的Sql更容易书写
    • 为异常信息记录、值转换器安装和数据映射提供钩子。(Hooks for logging exceptions, installing value converters and mapping columns to properties without attributes.)
    • 兼容SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle。
    • 可以在.NET 3.5 或Mono 2.6或更高版本上运行
    • 在.NET 4.0 和Mono 2.8下支持dynamic
    • NUnit单元测试
    • 开源(Apache License)
    • 所有功能大约用了1500行代码

    如何获取PetaPoco?

    因为中国使用win7系统的比较多,然后win7自带.net3.5框架,所以笔者从nuget下载了4.0.3版本的PetaPoco

    获取地址:

    可以和笔者一样安装4.0.3版本

    如下图,回车即可

     安装之后,如果你是.net3.5的编译环境,请关闭关闭dynamic支持:

    1、打开项目属性

    2、切换到“生成”选项卡

    3、在“条件编译符号”添加PETAPOCO_NO_DYNAMIC

    添加应用程序配置文件app.config

    在文件里面填写sql链接参数:

    我这里用的是SqlServer数据库

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="connectionString" connectionString="Data Source=.sql2008;Initial Catalog=test;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    </configuration>
    复制代码

    然后在T4模板中,填上刚才加的链接参数,一保存就可以自动生成对应的实体

    如图

    下面贴出一部分测试代码

    复制代码
    using PetaPocoDemo.Models;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace PetaPocoDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //创建一个petapoco对象
                var db = new PetaPoco.Database("connectionString");
    
                //遍历查询文章表
                foreach (var a in db.Query<article>("select * from article"))
                {
                    MessageBox.Show(a.article_id + "-------" + a.title);
                }
                //返回一个scalar数量
                var count = db.ExecuteScalar<int>("select count(*) from article");
                MessageBox.Show("count:" + count.ToString());
    
                //返回一行记录
                var row = db.SingleOrDefault<article>("select * from article where article_id='1'");
                MessageBox.Show(row.content);
    
                //插入记录
                var newArticle = new article();
                newArticle.article_id = 2;
                newArticle.title = "绿书";
                newArticle.content = "可持续发展绿色内容";
                newArticle.date_created = DateTime.UtcNow;
               
                if (Convert.ToInt32(db.Insert("article", "article_id",false, newArticle)) > 0)
                {
                    MessageBox.Show("sucess");
                }
                else
                {
                    MessageBox.Show("fail");
                }
            }
        }
    }
    复制代码
  • 相关阅读:
    Mac 卸载MySql的方法
    关于mysql的wait_timeout参数 设置不生效的问题
    linux下利用nohup后台运行jar文件包程序
    MySql创建视图
    spring mvc获取header
    Spring Data Jpa 查询返回自定义对象
    Caused by: org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must end with the ';' delimiter.
    eclipse Reference 功能之——项目之间的引用
    Mac 在启动eclipse时 Failed to load JavaHL Library解决方法
    MySQL Workbench update语句错误Error Code: 1175.
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/9963631.html
Copyright © 2020-2023  润新知