SaveChanges (false), savechangesfalse

Source: Internet
Author: User

SaveChanges (false), savechangesfalse

This is a problem I encountered when I was working on the project. After searching for half a day, I found an english blog and introduced it. I still don't understand it. Below are some of my translations of the original article, if there are any inconveniences, please criticize and correct them.

In many cases, the EF Entity Framework can manage transactions for you.

Each time you add an object to your. NET code, delete an object, modify an object, create a link, or delete a link. All are saved through EF. When you call the SaveChange () function, it is converted to a native SQL statement and executed in the database.

In these cases, you need to use your own transactions:

1. In the same transaction, a message is put into the Message Queue using the object context.

2. Use two object contexts at the same time.

3. Wait, you know ......

If a more serious error occurs and is not within the scope of the EF framework, you need to use TransactionScope to recover it ).

If you call SaveChange () or SaveChange (true), EF will directly think that everything is going well, then it will stop tracking changes and wait for new changes.

In this case, once an error occurs elsewhere in the transaction, we will not be able to recover because we have abandoned the tracking of changes.

Next we will introduce SaveChange (false) and AcceptAllChanges ().

SaveChange (false) tells EF the database command that must be executed, but insist on the change (here I do not quite understand the original statement is: but hold to the changes), so it can be reloaded if necessary. Now, if the entire transaction fails, you can retry EF location or call SaveChange (false ). Alternatively, you can use status management to record failures.

Once more transactions are successfully executed, you only call AcceptAllChanges () directly, and the tracked changes will be lost.

The typical pseudocode is as follows:

<span style="font-family:KaiTi_GB2312;font-size:18px;">using (TransactionScope scope = new TransactionScope()) {     //Do something with context1     //Do something with context2     //Save Changes but don't discard yet     context1.SaveChanges(false);     //Save Changes but don't discard yet     context2.SaveChanges(false);     //if we get here things are looking good.     scope.Complete();     //If we get here it is save to accept all changes.     context1.AcceptAllChanges();     context2.AcceptAllChanges(); }</span>
If a module is lost, it will cause all retry due to an exception. Is it meaningful?

Related Article

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.