2011-06-08 17 views
6

İki tablom var. Bu çerçeveyi varlık çerçevesini kullanarak güncelliyorum. işte benim kodumVarlık çerçevesi içinde işlem nasıl uygulanır

public bool UpdateTables() 
{ 
     UpdateTable1(); 
     UpdateTable2(); 
} 

Herhangi bir tablo güncelleme işlemi başarısız olursa, başka hiçbir şey yapılmamalıdır.

cevap

14
using (TransactionScope transaction = new TransactionScope()) 
{ 
    bool success = false; 
    try 
    { 
     //your code here 
     UpdateTable1(); 
     UpdateTable2(); 
     transaction.Complete(); 
     success = true; 
    } 
    catch (Exception ex) 
    { 
     // Handle errors and deadlocks here and retry if needed. 
     // Allow an UpdateException to pass through and 
     // retry, otherwise stop the execution. 
     if (ex.GetType() != typeof(UpdateException)) 
     { 
      Console.WriteLine("An error occured. " 
       + "The operation cannot be retried." 
       + ex.Message); 
      break; 
     } 
    }  

    if (success) 
     context.AcceptAllChanges(); 
    else  
     Console.WriteLine("The operation could not be completed"); 

    // Dispose the object context. 
    context.Dispose();  
} 
+0

Teşekkürler. Ben varlık çerçeveleme kullanarak veritabanı işlemi gerçekleştiren WCF servisini kullanıyorum. Bu yöntem işe yaramazsa bu senaryoda çalışmak? – Tuscan

+0

Evet, TransactionScope bir kod bloğu işlem yapar (her yerde kullanılır). TransactionScope, nesne bağlamındaki nesnelere yapılan değişikliklerin bir mesaj kuyruğu ile koordine edilmesini sağlar. – Akhil

+0

thanx bir kez daha – Tuscan

4

kullanım TransactionScope

ayrıca proje refference için System.Transactions eklemek gerek
public bool UpdateTables() 
    { 
     using (System.Transactions.TransactionScope sp = new System.Transactions.TransactionScope()) 
     { 
      UpdateTable1(); 
      UpdateTable2(); 
      sp.Complete(); 
     } 
    } 

0

Bir TransactionScope kullanmak gerekmez: Varlık Framework otomatik dediğimiz bir işlem zorlar Bağlamınızda SaveChanges().

public bool UpdateTables() 
{ 
    using(var context = new MyDBContext()) 
    { 
     // use context to UpdateTable1(); 
     // use context to UpdateTable2(); 

     context.SaveChanges(); 
    } 
} 
0

Böyle bir şey yapabiliriz .... hızlı cevap için

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead })) 
       { 
        using (YeagerTechEntities DbContext = new YeagerTechEntities()) 
        { 
         Category category = new Category(); 

         category.CategoryID = cat.CategoryID; 
         category.Description = cat.Description; 

         // more entities here with updates/inserts 
         // the DbContext.SaveChanges method will save all the entities in their corresponding EntityState 

         DbContext.Entry(category).State = EntityState.Modified; 
         DbContext.SaveChanges(); 

         ts.Complete(); 
        } 
       }