C#的EF中使用數據庫事務和併發隔離級別


有時候我們需要直接在使用EF操作數據庫的C#程序中使用數據庫事務:

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required,
    new TransactionOptions() { IsolationLevel= System.Transactions.IsolationLevel.ReadUncommitted}))
{

}

以上代碼還設置事務中使用NOLOCK的隔離級別,就是允許髒讀,要改變隔離級別,可以修改
IsolationLevel
的參數。注意,EF使用事務要引用 System.Transactions;

關於隔離級別還可以直接使用

 using (DaDbContext db = new DaDbContext())
            {
                db.Database.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
}


發佈了35 篇原創文章 · 獲贊 21 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章