2015-11-12 71 views
2

我試圖在域級別使用TransactionScope,所以我可以跨越(可能)在多個存儲庫上操作數據,但將所有數據保存在同一個事務中。實體框架:TransactionScope有一個不同的IsolationLevel

我有以下保存方法:

public void Save(MyEntity source) 
{ 
    using (var scope = new TransactionScope()) 
    { 
     var context = new MyEFEntities(environment.ConnectionString); 

     this.Repository.Add(source.ToDbMyEntity(), context); 

     context.SaveChanges(); 

     scope.Complete(); 
    } 
} 

,但我得到的.SaveChanges()以下錯誤:

爲的TransactionScope指定的交易具有比請求的值不同 的IsolationLevel範圍。參數名稱: transactionOptions.IsolationLevel

這是什麼原因造成的?

+0

你能告訴我你的數據庫當前的'隔離級別? http://stackoverflow.com/questions/1038113/how-to-find-current-transaction-level – Alireza

回答

3

我認爲對於實體框架默認隔離級別是一個這是默認的數據庫,使用Sql Server實例默認隔離級別爲READ COMMITTED,而TransactionScope的默認值是Serializable所以當你試圖改變它裏面using (var scope = new TransactionScope())塊它拋出一個錯誤。嘗試這樣的:

var transactionOptions = new TransactionOptions(); 
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; 
using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions)); 
相關問題