I have a 2 DbContext'
s that I call both of them in one form as you can see here:
dbcontext1.add(object);
dbcontext1.save();
dbcontext2.add(object);
dbcontext2.save();
So I have a question: if some problems happen and my record saved by DbContext1
but not in DbContext2
, I need to rollback my transaction.
I searched and I found two methods: using BeginTransaction
and TransactionScope
. I am confused about which one I should use and what the differences between them are?
I found something like this but i don;'t know how can i roll back this :
using (TransactionScope scope = new TransactionScope())
{
using (EFEntities1 dc = new EFEntities1())
{
dc.USERS.Add(user);
dc.SaveChanges();
}
using (EFEntities2 dc = new EFEntities2())
{
dc.USERS.Add(user);
dc.SaveChanges();
}
scope.complete();
}
Best regards
A DbContextTransaction object provides Commit() and Rollback() methods to do commit and rollback on the underlying store transaction. This method requires an open underlying stored connection. This method opens a connection if it is not already open. This method will close the connection when Dispose () is called.
Sometimes though the SaveChanges(false) + AcceptAllChanges() pairing is useful. The most useful place for this is in situations where you want to do a distributed transaction across two different Contexts. If context1. SaveChanges() succeeds but context2.
The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.
UseTransaction() method allows us to use an existing transaction created out of the scope of the context object.
I am confused about which one I should use and what the differences between them are?
The difference is that TransactionScope
behaves like a regular, lightweight and local transaction as long as you use it with a single database connection (SQL Server 2005) or with multiple connections on the same database (SQL Server 2008 and up). If the two or more connections are used within the same transaction scope or more than one databases are accessed (again, depending on SQL Server version), then it is promoted to a distributed transaction (so registered with MSDTC). In your case, it will be promoted.
I found something like this but i don;'t know how can i roll back this.
As long as you don't call Complete
, the transaction scope is rolled back as soon as the scope ends (e.g. dispose, the using ends, etc).
If an exception is thrown by either of the db contexts, then in your code Complete
will not be called and the transaction is going to be rolled back.
From MSDN:
If no exception occurs within the transaction scope (that is, between the initialization of the TransactionScope object and the calling of its Dispose method), then the transaction in which the scope participates is allowed to proceed. If an exception does occur within the transaction scope, the transaction in which it participates will be rolled back.
This is also the reason why a Rollback
method does not exist.
You might also want to look into what isolation level to use with it. The default is Serializable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With