博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dapper学习(四)之Dapper Plus的大数据量的操作
阅读量:5038 次
发布时间:2019-06-12

本文共 5778 字,大约阅读时间需要 19 分钟。

这篇文章主要讲 Dapper Plus,它使用用来操作大数量的一些操作的。比如插入1000条,或者10000条的数据时,再使用Dapper的Execute方法,就会比较慢了。这时候,可以使用Dapper Plus中的方法进行操作,提高速度。

主要包括下面:

  • Bulk Insert
  • Bulk Update
  • Bulk Delete
  • Bulk Merge

使用之前,需要在Nuget中,安装 Z.Dapper.Plus 

1. Bulk Insert:批量插入

1.1 Insert Single : 使用Bulk插入单个实体

DapperPlusManager.Entity
().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkInsert(new List
() { new Customer() { CustomerName = "ExampleBulkInsert", ContactName = "Example Name :" + 1}});}

1.2 Insert Many :插入多个实体

DapperPlusManager.Entity
().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkInsert(customers);}

1.3 Insert with relation(One to One)

插入一对一关系的实体

DapperPlusManager.Entity
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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
().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity
().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
().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
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().Table("Products").Identity(x => x.ProductID);using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkUpdate(suppliers, x => x.Product);}

更新一对多关系的实体

DapperPlusManager.Entity
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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
().Table("Customers").Key("CustomerID");using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkDelete(connection.Query
("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());}

3.2 Delete Many

删除多个实体

DapperPlusManager.Entity
().Table("Customers").Key("CustomerID");using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkDelete(connection.Query
("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());}

3.3 Delete with relation(One to One)

删除一对一关系的实体

DapperPlusManager.Entity
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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
().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkMerge(new List
() { new Customer() { CustomerName = "ExampleBulkMerge", ContactName = "Example Name :" + 1}});}

4.2 Merge Many

DapperPlusManager.Entity
().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){ connection.BulkMerge(customers);}

4.3 Merge with relation(One to One)

DapperPlusManager.Entity
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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
().Table("Suppliers").Identity(x => x.SupplierID);DapperPlusManager.Entity
().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);}

 

转载于:https://www.cnblogs.com/Vincent-yuan/p/11521373.html

你可能感兴趣的文章
JavaSript模块规范 - AMD规范与CMD规范介绍
查看>>
都市环游
查看>>
【工具】【截图工具】FScapture,支持滚动
查看>>
jQuery延迟加载(懒加载)插件 – jquery.lazyload.js
查看>>
人脸检测(1)——HOG特征
查看>>
react native 示例代码
查看>>
关于V1.6.0版本的项目总结
查看>>
想想还要做哪些事。。。
查看>>
python字典顺序转字符串
查看>>
面试题思考:web中关于一些容器基本概念的简单总结
查看>>
计算机专业顶级学术会议
查看>>
无乱码截取字符串,中英文混合(转)
查看>>
python-13常用内建模块
查看>>
【Spring】Spring系列7之Spring整合MVC框架
查看>>
POJ2195&&HDU1533(KB11-D 最小费用最大流)
查看>>
symfony2 表单
查看>>
微信小程序调用后台接口+热点新闻滚动展示
查看>>
【实例解析】某水泥企业应用商业智能提升管理效率
查看>>
如何利用自定义函数把阳历转换成阴历
查看>>
declare和typeset DEMO
查看>>