在前一篇中咱们讲到了Dapper的应用,可是给咱们的感受Dapper不像个ORM更像一个IDbConnection的扩展。是的,没错。在实际开发中咱们常常用Dapper做为对EF Core的补充。固然了Dapper并不单单只有这些,就让咱们经过这一篇文章去让Dapper更像一个ORM吧。数据库
Dapper Contrib 扩展了Dapper对于实体类的CRUD方法:app
安装方法:asp.net
命令行:ide
dotnet add package Dapper.Contrib
NuGet:测试
Install-Package Dapper.Contrib
使用:this
using Dapper.Contrib.Extensions;
这个是一个使得Dapper功能更强大的扩展包,由于支持了CRUD,因此须要对实体类添加配置,该扩展包使用Attribute做为依据进行相关映射配置:spa
[Table("Model")]
public class Model
{
[Key]
[ExplicitKey]
public int Id{get;set;}
[Computed]
public int Count {get;set;}
[Write]
public String Name{get;set;}
}
这是全部的配置,Table用来声明是一个表,必须指定表名,Key表示该属性是数据库主键,ExplicitKey表示这个属性是数据库中显示设置的主键,Computed表示该字段是一个计算字段,Write表示该字段能够设置值进去。须要注意的是:Key和ExplicitKey这两个不能同时标注在一个属性上。.net
那么接下来,咱们看看它扩展了哪些方法:插件
插入单个对象:命令行
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
其中 transcation表示事务,若是指定事务,数据的提交将由事务控制,该方法会返回插入对象的主键(若是对象主键是数字类型)或者返回一个待插入列表中已插入的行数。
获取单个对象:
public static T Get<T>(this IDbConnection connection, [Dynamic] dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
经过传入主键,获取一个数据
获取全部数据:
public static IEnumerable<T> GetAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
更新数据:
Dapper Contrib 提供了一个用来更新的方法:
public static bool Update<T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
这个方法比较有意思的是
var entity = connection.Get<Model>(1);
entity.Name = "测试1";
connection.Update(entity);
和
var models = connection.GetAll<Model>();
foreach(var m in models)
{
Console.WriteLine(m);
m.StringLength ++;
}
connection.Update(models.AsList());
均可以,并不会报错。
不过须要注意的是,若是须要更新的实例没有指定主键值(主键属性没有赋值),则不会有任何行发生更新。并且在更新的时候,会更新全部列,不会由于不赋值就不更新。
删除方法有两个:
public static bool Delete<T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
public static bool DeleteAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
删除也是传入一个实体类,同样也只是须要主键有值,若是没有找到主键对应的数据,则不会有任何变化。Delete与Update同样,若是传入一个List集合也是能够的。
这个包扩展了Dapper的事务处理能力。虽然是Dapper的扩展包,可是是给IConnection添加了一个扩展方法。使用示例以下:
dotnet add package Dapper.Transaction
老规矩,记得先把包加进来。
而后代码是这样的:
using Dapper.Transaction;
using(var connection = new SqliteConnection("Data Source=./demo.db"))
{
connection.Open();
var transcation = connection.BeginTransaction();
// 编写业务代码
transcation.Commit();
}
若是使用Dapper Transaction,须要先调用 connection.Open()来确保链接是开启状态。
transcation这个对象能够当作普通的DbTranscation对象,传给Dapper的方法来使用,也能够当作一个开启了事务的Dapper客户端来使用。也就是说,Dapper对IDbConnection扩展的方法,在这个包对IDbTranscation也扩展了响应的方法:
这个插件是Dapper上用来处理巨量数据的插件,但这是个收费版的插件,不过每月都有必定的试用期限。想试试的能够下一下:
dotnet add package Z.Dapper.Plus
使用:
using Z.Dapper.Plus;
这个插件在使用以前须要先配置实体类与数据库之间的映射关系:
DapperPlusManager.Entity<Customer>().Table("Customers");
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
该插件支持四组大批量处理方式:
Bulk Insert
Bulk Update
Bulk Merge
Bulk Delete
// STEP MAPPING
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
// STEP BULKINSERT
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkInsert(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkInsert(x => x.Products);
}
// STEP BULKUPDATE
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkUpdate(suppliers, x => x.Products);
}
// STEP BULKMERGE
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkMerge(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkMerge(x => x.Products);
}
// STEP BULKDELETE
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkDelete(suppliers.SelectMany(x => x.Products)).BulkDelete(suppliers);
}
这些插件让Dapper更强,也更具有一个完整的ORM的方法,固然实际开发中须要结合实际需求使用。可能并非全部的都合适。
Dapper的内容就到此为止了。原本预计下一篇开始 asp.net core的内容,不过有个小伙伴推荐了FreeSql,我看了下感受挺不错的,就给小伙伴们介绍一下~这一个介绍完成以后,就进入了我期待已久的asp.net core系列了。