Dapper学习(四)之Dapper Plus的大数据量的操作
这篇文章主要讲 Dapper Plus,它使用用来操作大数量的一些操作的。比如插入1000条,或者10000条的数据时,再使用Dapper的Execute方法,就会比较慢了。这时候,可以使用Dapper Plus中的方法进行操作,提高速度。
主要包括下面:
- Bulk Insert
- Bulk Update
- Bulk Delete
- Bulk Merge
使用之前,需要在Nuget中,安装 Z.Dapper.Plus
注意:这个组件是收费的,每个月会有一个试用版本,没有免费版本
另外一种提高批量插入速度的方式:通过把多个插入语句通过字符串拼接使成为一个长的sql语句来实现。
1. Bulk Insert:批量插入
1.1 Insert Single : 使用Bulk插入单个实体
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkInsert(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkInsert", ContactName = "Example Name :" + 1}}); }
1.2 Insert Many :插入多个实体
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkInsert(customers); }
1.3 Insert with relation(One to One)
插入一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkInsert(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkInsert(x => x.Product); }
1.4 Insert with relation (One to Many)
插入一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkInsert(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkInsert(x => x.Products); }
2. Bulk Update
2.1 Update Single 和 Update Many
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(customers); }
2.2 Update with relation(One to One) 和 Update with relation(One to Many)
更新一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(suppliers, x => x.Product); }
更新一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(suppliers, x => x.Products); }
3. Bulk Delete
3.1 Delete Single
删除单个实体
DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList()); }
3.2 Delete Many
删除多个实体
DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList()); }
3.3 Delete with relation(One to One)
删除一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkDelete(suppliers.Select(x => x.Product)).BulkDelete(suppliers); }
3.4 Delete with relation(One to Many)
删除一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkDelete(suppliers.SelectMany(x => x.Products)).BulkDelete(suppliers); }
4.Bulk Merge
4.1 Merge Single
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkMerge(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkMerge", ContactName = "Example Name :" + 1}}); }
4.2 Merge Many
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkMerge(customers); }
4.3 Merge with relation(One to One)
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkMerge(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkMerge(x => x.Product); }
4.4 Merge with relation(One to Many)
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkMerge(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkMerge(x => x.Products); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现