A preliminary understanding of transaction processing in EF

Source: Internet
Author: User
Tags terminates

1. The EF encapsulates the transaction: the SaveChange () of the context is transactional.

2. Operations that rely on multiple different context (that is, distributed operations) or multiple calls to the context.savechanges () operation are separated from the EF transaction encapsulation and can be implemented using TransactionScope. Case on behalf of:

[CSharp]View Plaincopy
  1. using (TransactionScope scope = new TransactionScope ())
  2. {
  3. //do something with Context1
  4. //do something with Context2
  5. //save and discard changes
  6. Context1. SaveChanges ();
  7. //save and discard changes
  8. Context2. SaveChanges ();
  9. //if We get here things is looking good.
  10. Scope.complete ();
  11. }

But it is risky to write, if Context1. SaveChanges () succeeded, context2. SaveChanges () fails and terminates when Scope.complete () commits the transaction, and CONTEXT1 has successfully executed a requirement that may not necessarily be met. If you need to context1, context2 or not succeed at the same time, or not successful, you need to make a small adjustment to the code, such as the following code:

[CSharp]View Plaincopy
  1. using (TransactionScope scope = new TransactionScope ())
  2. {
  3. //do something with Context1
  4. //do something with Context2
  5. //save Changes but don ' t discard yet
  6. Context1.  SaveChanges (false);
  7. //save Changes but don ' t discard yet
  8. Context2.  SaveChanges (false);
  9. //if We get here things is looking good.
  10. Scope.complete ();
  11. Context1. AcceptAllChanges ();
  12. Context2. AcceptAllChanges ();
  13. }


Using SaveChanges (false) to first send the necessary database Operations command to the database, it is important to note that CONTEXT1 and context2 have not really changed, if the transaction terminates, automatic rollback, both of the changes are not actually committed to the database, so it can be successfully rolled back.

TransactionScope's understanding:

1. Not only in database transactions, but also other types of transactions can be managed, the function is very powerful, poor performance

2. You should use the same context for database operations as much as possible, for reasons:

Saving resources and not creating a context is a resource-intensive operation.

Using the same context in different Dal can also achieve transactional purposes, so TransactionScope is not necessary in general database transactions

Resources

Http://www.cnblogs.com/aisini/archive/2011/03/25/1994487.html

A preliminary understanding of transaction processing in EF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.