Reputation: 2407
I use Linq-to-Entity (EF 4.0) in my project. I want to use transactions in My Code. i found two way to use transactions in my code. What is the difference between System.Transaction and System.Data.Common.DbTransaction? and which is performance better?
first:
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();
}
}
second:
using (testEntities ent = new testEntities())
{
ent.Connection.Open();
using (TransactionScope tscope= new TransactionScope())
{
...
int er1 = ent.SaveChanges();
...
int er2 = ent.SaveChanges();
tscope.Complete();
}
}
Upvotes: 1
Views: 2474
Reputation: 1010
From msdn
The System.Transactions namespace contains classes that allow you to write your own transactional application and resource manager. Specifically, you can create and participate in a transaction (local or distributed) with one or multiple participants.
DbTransactions are for database transactions only. TransactionScope gives you automatic transaction enrollment ability. It will enroll or create a new transactions for you. This can inlude database or distributed transactions.
Check the msdn for more information.
Upvotes: 4