RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#中让Dapper更强的插件是什么

本篇内容主要讲解“C#中让Dapper更强的插件是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#中让Dapper更强的插件是什么”吧!

10年的翁牛特网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整翁牛特建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“翁牛特网站设计”,“翁牛特网站推广”以来,每个客户项目都认真落实执行。

1. Dapper Contrib

Dapper Contrib 扩展了Dapper对于实体类的CRUD方法:

安装方法:

命令行:

dotnet add package Dapper.Contrib
 

NuGet:

Install-Package Dapper.Contrib
 

使用:

using Dapper.Contrib.Extensions;
 

这个是一个使得Dapper功能更强大的扩展包,因为支持了CRUD,所以需要对实体类添加配置,该扩展包使用Attribute作为依据进行相关映射配置:

[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这两个不能同时标注在一个属性上。

那么接下来,我们看看它扩展了哪些方法:

插入单个对象:

public static long Insert(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
 

其中 transcation表示事务,如果指定事务,数据的提交将由事务控制,该方法会返回插入对象的主键(如果对象主键是数字类型)或者返回一个待插入列表中已插入的行数。

获取单个对象:

public static T Get(this IDbConnection connection, [Dynamic] dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
 

通过传入主键,获取一个数据

获取所有数据:

public static IEnumerable GetAll(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
 

更新数据:

Dapper Contrib 提供了一个用来更新的方法:

public static bool Update(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
 

这个方法比较有意思的是

var entity = connection.Get(1);
entity.Name = "测试1";
connection.Update(entity);
 

var models = connection.GetAll();
foreach(var m in models)
{
   Console.WriteLine(m);
   m.StringLength ++;
}
connection.Update(models.AsList());
 

都可以,并不会报错。

不过需要注意的是,如果需要更新的实例没有指定主键值(主键属性没有赋值),则不会有任何行发生更新。而且在更新的时候,会更新所有列,不会因为不赋值就不更新。

删除方法有两个:

public static bool Delete(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
public static bool DeleteAll(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
 

删除也是传入一个实体类,一样也只是需要主键有值,如果没有找到主键对应的数据,则不会有任何变化。Delete与Update一样,如果传入一个List集合也是可以的。

 

2. Dapper Transaction

这个包扩展了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也扩展了响应的方法:

C#中让Dapper更强的插件是什么

 

3. Dapper Plus

这个插件是Dapper上用来处理巨量数据的插件,但这是个收费版的插件,不过每个月都有一定的试用期限。想试试的可以下一下:

dotnet add package Z.Dapper.Plus
 

使用:

using Z.Dapper.Plus;
 

这个插件在使用之前需要先配置实体类与数据库之间的映射关系:

DapperPlusManager.Entity().Table("Customers");
DapperPlusManager.Entity().Table("Suppliers").Identity(x => x.SupplierID);
 

该插件支持四组大批量处理方式:

  • Bulk Insert

  • Bulk Update

  • Bulk Merge

  • Bulk Delete

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

到此,相信大家对“C#中让Dapper更强的插件是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网页标题:C#中让Dapper更强的插件是什么
文章转载:http://scyingshan.cn/article/pjochs.html