プロジェクトで Linq-to-Entity (EF 4.0) を使用しています。マイコードでトランザクションを利用したい。コードでトランザクションを使用する 2 つの方法を見つけました。System.Transaction と System.Data.Common.DbTransaction の違いは何ですか? どちらがパフォーマンスが優れていますか?
最初:
using (testEntities ent = new testEntities())
{
ent.Connection.Open();
using (System.Data.Common.DbTransaction transaction = ent.Connection.BeginTransaction())
{
try
{
...
int er1 = ent.SaveChanges();
if (er1 > 0)
success = true;
else
success = false;
...
int er2 = ent.SaveChanges();
if (er2 > 0)
success = true;
else
success = false;
}
catch
{
success = false;
}
success = false;
if (success)
transaction.Commit();
else
transaction.Rollback();
}
}
2番目:
using (testEntities ent = new testEntities())
{
ent.Connection.Open();
using (TransactionScope tscope= new TransactionScope())
{
...
int er1 = ent.SaveChanges();
...
int er2 = ent.SaveChanges();
tscope.Complete();
}
}