近端时间从推酷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");
}
}
}
}

ORM之PetaPoco的更多相关文章

  1. 【译】微型ORM:PetaPoco【不完整的翻译】

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...

  2. C#轻型ORM框架PetaPoco试水

    近端时间从推酷app上了解到C#轻微型的ORM框架--PetaPoco.从github Dapper 开源项目可以看到PetaPoco排第四 以下是网友根据官方介绍翻译,这里贴出来. PetaPoco ...

  3. 【译】微型ORM:PetaPoco

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...

  4. 【译】微型ORM:PetaPoco【不完整的翻译】(转)

    出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...

  5. 微型ORM:PetaPoco 学习资料整理

    github地址:https://github.com/CollaboratingPlatypus/PetaPoco petapoco 实体中字段去掉关联(类似于EF中的NotMap) 微型ORM:P ...

  6. ORM之PetaPoco入门(二)--Petapoco基本用法

    1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...

  7. ORM之PetaPoco入门(一)--Petapoco简介

    1. ORM概括 1.1. ORM简介 ORM 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应 ...

  8. ORM之PetaPoco错误--VS中NUGet程序包管理安装PetaPoco

    一般在Vs中使用PetaPoco的时候都是使用NuGet程序包管理来安装PetaPoco的,如果你在安装PetaPoco前设置了ConnectionString,那么PetaPoco中的T4模板会自动 ...

  9. .NET Core ORM 类库Petapoco中对分页Page添加Order By对查询的影响

    最近一直在使用Petapoco+Entity Framework Core结合开发一套系统. 使用EFCore进行Code First编码,使用PMC命令生成数据库表的信息. 使用Petapoco进行 ...

随机推荐

  1. ror笔记2

    在rails app的 config 文件夹中新建unicorn.rb内容如下 worker_processes 2 working_directory "/home/mage/boleht ...

  2. MySQL两个日期字段相减得到秒的方法

    一.MySQL中两个DateTime字段相减 假定表名为tblName,两个DateTime字段名分别为beginDateTime,endDateTime,以下是相关两个mysql日期字段相减的SQL ...

  3. Linux 如何杀死僵尸进程

    问题描述: shell > top top - :: up days, :, user, load average: 0.23, 0.81, 1.07 Tasks: total, running ...

  4. inline-block元素出现位置错位的解决方法

    如下代码所示: <div class="container"> <div style="display: inline-block; height: 1 ...

  5. Python与Go冒泡排序

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 冒泡排序法 def bubbling(array): # 时间复杂度:O(n^2) for i in r ...

  6. Unmarshaller解析xml文件

    参考地址:http://linbulu.iteye.com/blog/2295919 Girl.xml文件 <?xml version="1.0" encoding=&quo ...

  7. Unity iOS 项目的一种性能评测方法

    [Unity iOS 项目的一种性能评测方法]

  8. Fresnel Reflection Shader

    [Fresnel Reflection] One of the most used types of reflections is the Fresnel reflection. One of the ...

  9. Unity中Avatar换装实现

    http://www.cnblogs.com/herenzhiming/articles/6533162.html

  10. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...