Entity Framework - 控制Entity Transaction

Entity Transaction方法有:

  • EntityTransaction - 執行EntityCommand交易時使用,由EntityConneciton取得.
  • Transaction
  • TransactionScope

Transaction Scope Sample Code

bool Success=true;
using (TransactionScope transaction = new TransactionScope()){
    try{
        // do something
        context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
        transaction.Complete();
    }
    catch(Exception ex){
        bool Success=false;
    }
}
if(Success){
    context.AcceptAllChanges();
}

沒有留言:

橫式廣告