溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中讓Dapper更強的插件是什么

發(fā)布時間:2021-11-24 13:47:10 來源:億速云 閱讀:180 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“C#中讓Dapper更強的插件是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#中讓Dapper更強的插件是什么”吧!

1. Dapper Contrib

Dapper Contrib 擴展了Dapper對于實體類的CRUD方法:

安裝方法:

命令行:

dotnet add package Dapper.Contrib
 

NuGet:

Install-Package Dapper.Contrib
 

使用:

using Dapper.Contrib.Extensions;
 

這個是一個使得Dapper功能更強大的擴展包,因為支持了CRUD,所以需要對實體類添加配置,該擴展包使用Attribute作為依據(jù)進行相關(guān)映射配置:

[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表示該屬性是數(shù)據(jù)庫主鍵,ExplicitKey表示這個屬性是數(shù)據(jù)庫中顯示設(shè)置的主鍵,Computed表示該字段是一個計算字段,Write表示該字段可以設(shè)置值進去。需要注意的是:Key和ExplicitKey這兩個不能同時標注在一個屬性上。

那么接下來,我們看看它擴展了哪些方法:

插入單個對象:

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

其中 transcation表示事務(wù),如果指定事務(wù),數(shù)據(jù)的提交將由事務(wù)控制,該方法會返回插入對象的主鍵(如果對象主鍵是數(shù)字類型)或者返回一個待插入列表中已插入的行數(shù)。

獲取單個對象:

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

通過傳入主鍵,獲取一個數(shù)據(jù)

獲取所有數(shù)據(jù):

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

更新數(shù)據(jù):

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());
 

都可以,并不會報錯。

不過需要注意的是,如果需要更新的實例沒有指定主鍵值(主鍵屬性沒有賦值),則不會有任何行發(fā)生更新。而且在更新的時候,會更新所有列,不會因為不賦值就不更新。

刪除方法有兩個:

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;
 

刪除也是傳入一個實體類,一樣也只是需要主鍵有值,如果沒有找到主鍵對應的數(shù)據(jù),則不會有任何變化。Delete與Update一樣,如果傳入一個List集合也是可以的。


 

2. Dapper Transaction

這個包擴展了Dapper的事務(wù)處理能力。雖然是Dapper的擴展包,但是是給IConnection添加了一個擴展方法。使用示例如下:

dotnet add package Dapper.Transaction
 

老規(guī)矩,記得先把包加進來。

然后代碼是這樣的:

using Dapper.Transaction;
 
using(var connection = new SqliteConnection("Data Source=./demo.db"))
{
   connection.Open();
   var transcation = connection.BeginTransaction();
   // 編寫業(yè)務(wù)代碼
   transcation.Commit();
}
 

如果使用Dapper Transaction,需要先調(diào)用 connection.Open()來確保連接是開啟狀態(tài)。

transcation這個對象可以當做普通的DbTranscation對象,傳給Dapper的方法來使用,也可以當做一個開啟了事務(wù)的Dapper客戶端來使用。也就是說,Dapper對IDbConnection擴展的方法,在這個包對IDbTranscation也擴展了響應的方法:

C#中讓Dapper更強的插件是什么

 

3. Dapper Plus

這個插件是Dapper上用來處理巨量數(shù)據(jù)的插件,但這是個收費版的插件,不過每個月都有一定的試用期限。想試試的可以下一下:

dotnet add package Z.Dapper.Plus
 

使用:

using Z.Dapper.Plus;
 

這個插件在使用之前需要先配置實體類與數(shù)據(jù)庫之間的映射關(guān)系:

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);
}

到此,相信大家對“C#中讓Dapper更強的插件是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI