1、简介
PetaPoco是一个小型、快速、单文件的微型ORM(Object Relational Mapper)框架,可在.NET和Mono环境运行。
https://github.com/CollaboratingPlatypus/PetaPoco.
2、特点
PetaPoco 是一个用于 .NET 的小型且快速的微型 ORM
-
与Dapper一样,它很快,因为它使用动态方法生成 (MSIL) 将列值分配给属性
-
像Massive一样,它现在也支持动态 Expandos
-
与ActiveRecord一样,它支持对象和数据库表之间的密切关系
-
与SubSonic一样,它支持使用 T4 模板生成 poco 类(仅限 V5)
-
与Massive一样,它以单个文件的形式提供,您可以轻松地将其添加到任何项目或编译。(仅限 V5)
3、功能
-
很小,绝对没有依赖!
-
异步或同步,选择权在您。(又名异步)(V6)
-
与严格未修饰的 POCO 或归属的几乎 POCO 一起使用。
-
易于配置,包括开箱即用的流畅配置。
-
插入/删除/更新/保存和 IsNew 的辅助方法
-
分页请求会自动计算出总记录数并获取特定页面。
-
简单的交易支持。
-
更好的参数替换支持,包括从对象属性中获取命名参数。
-
通过使用 DynamicMethod 生成消除 Linq 和快速属性分配来获得出色的性能。
-
查询语言是好的 ole SQL。
-
包括一个低摩擦的 SQL 构建器类,使编写内联 SQL变得更加容易。
-
包括 T4 模板以自动为您生成 POCO 类。(V5)
-
用于记录异常、安装值转换器和将列映射到没有属性的属性的挂钩。
-
适用于 SQL Server、SQL Server CE、MS Access、SQLite、MySQL、MariaDB、Firebird 和 PostgreSQL。(Oracle 支持但没有集成测试)。
-
在 Net Standard 2.0、.NET 4.0/4.5+ 或 Mono 2.8 及更高版本下工作。
-
有Xunit单元测试。
-
已支持 DBs 集成测试。
-
开源(MIT 许可证或 Apache 2.0)
4、简单例子
添加
[Fact]public void Insert(){// Create the personvar person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };// Tell PetaPoco to insert itvar id = DB.Insert(person);// Obviously the ID returned will be the same as the one we setid.ShouldBe(person.Id);// Get a clone/copy from the DBvar clone = DB.Single<Person>(id);// See, they're are the sameclone.ShouldBe(person);// But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM.person.Equals(clone).ShouldBeFalse();}
更新
// Create and insert the personvar person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };var id = DB.Insert(person);// Update a few properties of the personperson.Age = 70;person.Name = "The PetaPoco";// Tell PetaPoco to update the DBDB.Update(person);// Get a clone/copy from the DBvar clone = DB.Single<Person>(id);// See, the person has been updatedclone.Id.ShouldBe(person.Id);clone.Dob.ShouldBe(person.Dob);clone.Height.ShouldBe(person.Height);clone.Age.ShouldBe(person.Age);clone.Name.ShouldBe(person.Name);
删除
// Create the personvar person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };// Tell PetaPoco to insert itDB.Insert(person);// Obviously, we find only 1 matching person in the dbvar count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });count.ShouldBe(1);// Tell PetaPoco to delete itDB.Delete(person);// Obviously, we should now have none in the dbcount = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);count.ShouldBe(0);
查询
var inmemorylist = db.Fetch<article>("SELECT * FROM Articles")foreach (var a in inmemorylist){ Console.WriteLine($"{a.Id} - {a.Title}");}Console.WriteLine($"Count: {inmemorylist.Count}");foreach (var a in db.Query<article>("SELECT * FROM Articles")){ Console.WriteLine($"{a.Id} - {a.Title}");}
更多方法见
https://github.com/CollaboratingPlatypus/PetaPoco/wiki/