本文介紹了如何在實體框架中回滾事務的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
string[] usersToAdd = new string[] { "asd", "asdert", "gasdff6" };
using (Entities context = new Entities())
{
foreach (string user in usersToAdd)
{
context.AddToUsers(new User { Name = user });
}
try
{
context.SaveChanges(); //Exception thrown: user 'gasdff6' already exist.
}
catch (Exception e)
{
//Roll back all changes including the two previous users.
}
或者這可能是自動完成的,這意味著如果發生錯誤,則會取消所有更改的提交更改.是嗎?
Or maybe this is done automatically, meaning that if error occurs, committing changes are canceled for all the changes. is it?
推薦答案
OK
我創建了一個示例應用程序,就像我在數據庫中檢查的問題和后記中的示例一樣,沒有添加任何用戶.
I created a sample a application like the example from the the question and afterwords I checked in the DB and no users were added.
結論:ObjectContext.SaveChange 它自動是一個事務.
注意:如果執行 sprocs 等,我相信交易是需要的.
Note: I believe transactions will be needed if executing sprocs etc.
這篇關于如何在實體框架中回滾事務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!