c# - Using Transactions or SaveChanges(false) and AcceptAllChanges()? -
I am checking the transaction and it appears that as long as I false Code> Save Changes () and then call Accept all changes () if there are no errors:
Save Changes (false); // accept all changes (); What if something gets worse? Do I have to rollback or when my method goes out of the scope, the transaction ends?
What happened to any indentation column through the transaction, which was assigned half way? I believe that if someone else added my record after my bad, it means that there will be no false identity value.
Is there a reason to use the standard TransactionScope class in my code?
With most of the unit framework, SaveChanges () is enough. It makes any transaction, or lists any ambient transactions, and performs all the necessary functions in that transaction.
Sometimes, though Save Changes (false) + Accept all changes () is useful.
The most useful place for this is in situations where you want to make a distributed transaction in two different contexts.
Ie something (bad) like this: using
(TransactionScope scope = new TransactionScope ()) {// something with context1 / reference Do something / save / discard references and reference 1.SaveChanges (); // Save and discard references 2. Save Changes (); // If we meet here then things are looking good; scope.Complete (); } If context1.SaveChanges () succeeds but context2.SaveChanges () fails to cancel the entire distributed transaction has given. But unfortunately the unit framework has already abandoned the changes on context1 , so you can not run the failure again or log in effectively.
But if you want to change your code:
using (TransactionScope scope = new TransactionScope ()) {// something with context1 / reference Do something / save // save the changes yet. Save Changes (wrong); // Save changes, but do not leave the reference yet. Switch change (wrong); // If we meet here then things are looking good; scope.Complete (); Context1.AcceptAllChanges (); Context2.AcceptAllChanges (); } to call This means that the transaction actually throws an exception, either retrying or logging position of each references See more. ObjectState Manager / >. objectstate manager anywhere
Comments
Post a Comment