目前框架有以下
轻量级,以前单文件,目前有维护形成项目级别,适合多个数据库,开发入手比较快,二次开发扩展简单,模型 Emit 映射,数据交互需要 Code,并且需要编写脚本,接口上有自动翻页,支持多对象查询返回
- //保存对象
- db.Save(article);
- db.Save(new Article {
- Title = "Super easy to use PetaPoco"
- });
- db.Save("Articles", "Id", {
- Title = "Super easy to use PetaPoco",
- Id = Guid.New()
- });
- //获取一个对象
- var article = db.Single(123);
- var article = db.Single("WHERE ArticleKey = @0", "ART-123");
- //删除一个对象
- db.Delete(article);
- db.Delete(123);
- db.Delete("Articles", "Id", 123);
- db.Delete("Articles", "ArticleKey", "ART-123");
轻量级,单文件,支持多数据库,模型 Emit 反射,数据交互需要 Code,开发入手也比较快,二次开发扩展简单,支持多对象查询返回,优势在于写入数据比 PetaPoco 更加灵活
注意:所有扩展方法假定连接已打开,如果连接关闭,它们将失败
- //IDbConnection扩展查询
- public static IEnumerable < T > Query < T >(this IDbConnection conn,string sql,object param = null,SqlTransaction transaction = null,bool buffered = true)
- public class Dog {
- public int ? Age {
- get;
- set;
- }
- public Guid Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public float ? Weight {
- get;
- set;
- }
- public int IgnoredProperty {
- get {
- return 1;
- }
- }
- }
- //简单查询
- var guid = Guid.NewGuid();
- var dog = connection.Query("select Age = @Age, Id = @Id", new {
- Age = (int ? ) null,
- Id = guid
- });
- //验证统计数量
- dog.Count().IsEqualTo(1);
- //验证属性是否为null
- dog.First().Age.IsNull();
- //验证属性是否匹配
- dog.First().Id.IsEqualTo(guid);
- //查询两行数据
- var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
- //行一数据
- ((int)rows[0].A)
- .IsEqualTo(1);
- ((int)rows[0].B)
- .IsEqualTo(2);
- //行二数据
- ((int)rows[1].A)
- .IsEqualTo(3);
- ((int)rows[1].B)
- .IsEqualTo(4);
- public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
- connection.Execute(@"
- set nocount on
- create table #t(i int)
- set nocount off
- insert #t
- select @a a union all select @b
- set nocount on
- drop table #t", new {
- a = 1,
- b = 2
- }).IsEqualTo(2);
注意:如果是大批量插入不建议这么使用,请用 ADO.NET 自带的 BatchInsert
- connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", new[] {
- new {
- a = 1,
- b = 1
- },
- new {
- a = 2,
- b = 2
- },
- new {
- a = 3,
- b = 3
- }
- }).IsEqualTo(3);
非单文件,但也是轻量级,项目在持续维护,支持多数据库,和 dapper 和 PetaPoco 运用有点截然不同;它是用类对象继承 DynamicModel 来模拟实体对象的查询和写入,似乎没有看到多对象多表联合查询和返回,局限性还是比较小
- public class Products: DynamicModel {
- public Products() : base("northwind", "products", "productid") {}
- }
- var table = new Products();
- var products = table.All();
- //获取查询字段和搜索条件
- var productsFour = table.All(columns: "ProductName as Name", where: "WHERE categoryID=@0", args: 4);
- //分页查询
- var result = tbl.Paged(where: "UnitPrice > 20", currentPage: 2, pageSize: 20);
- var poopy = new {
- ProductName = "Chicken Fingers"
- };
- //更新 Product对象到表, 条件 ProductID = 12 ,设置 ProductName of "Chicken Fingers"
- table.Update(poopy, 12)
- //插入数据
- var table = new Categories();
- var inserted = table.Insert(new {
- CategoryName = "Buck Fify Stuff",
- Description = "Things I like"
- });
- //插入成功后得到返回新对象
- var newID = inserted.CategoryID;
项目已经比较老,目前已经 4-5 年未更新代码,里面类文件比较多,算不上轻量级,运用上比较简单
一种基于函数式编程理念的 Fluent ORM,项目今年少有更新,运用上几乎不写脚本,类似 EF 的数据对应模型开发。我们直接看 DEMO 运用吧
- //插入数据
- public int Insert(Employee employee)
- {
- return m_DataSource.Insert("HR.Employee", employee).ToInt32().Execute();
- }
- //更新数据
- public void Update(Employee employee)
- {
- m_DataSource.Update("HR.Employee", employee).Execute();
- }
- //初学者更新实体(但容易出问题,如果对应的漏写了一个属性的赋值)
- public void Update(Employee employee)
- {
- using (var context = new CodeFirstModels())
- {
- var entity = context.Employees.Where(e => e.EmployeeKey == employee.EmployeeKey).First();
- entity.CellPhone = employee.CellPhone;
- entity.FirstName = employee.FirstName;
- entity.LastName = employee.LastName;
- entity.ManagerKey = employee.ManagerKey;
- entity.MiddleName = employee.MiddleName;
- entity.OfficePhone = employee.OfficePhone;
- entity.Title = employee.Title;
- context.SaveChanges();
- }
- }
- //中级者更新
- public void Update(Employee employee)
- {
- using (var context = new CodeFirstModels())
- {
- context.Entry(employee).State = EntityState.Modified;
- context.SaveChanges();
- }
- }
- //获取指定表全部数据
- public IList GetAll()
- {
- return m_DataSource.From("HR.Employee").ToCollection().Execute();
- }
最后分析对比表,如果有异议,欢迎纠正
ORM 框架 | 难易度 | 开源 | 轻量级度 | 性能 | 扩展性 | 编码级 |
PetaPoco | 容易 | 是 | 是 | 快 | 方便 | 繁琐 |
Dapper | 容易 | 是 | 是 | 快 | 方便 | 繁琐 |
Massive | 中 | 是 | 是 | 快 | 一般 | 有点繁琐 |
Simple.Data | 容易 | 是 | 是 | 快 | 不用 | 简单 |
Chain | 容易 | 是 | 是 | 还可以 | 不用 | 简单 |
EF | 容易 | 否 | 否 | 一般 | 一般 | 简单 |
来源: http://www.cnblogs.com/rjf1979/p/6432874.html