This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23237|Reply: 1

[Source] Bulk submission and transaction processing in Entity Framework

[Copy link]
Posted on 4/8/2017 10:10:01 PM | | |

The use of SaveChanges() in Entity Framework is very frequent, and calling SaveChanges() after a single modification or deletion of data returns the number of impact records.

To use batch modification or batch deletion of data, you need the SaveChanges(false)+AcceptAllChanges() method.



SaveChanges(false) is just a notification that EF needs to perform an operation on the database, which is pending in memory and can be undone when necessary, such as AcceptAllChange() committing is truly successful, and EF will undo the operation of SaveChanges(false).



When dealing with distributed transaction operations, it is necessary to use TransactionScope to handle it, and many times we will write like this:


But writing like this is risky, false
For example, context1. SaveChanges() succeeded, context2. SaveChanges() is problematic, we are in scope. Complete() will terminate when the transaction is committed, and Context1 has been successfully executed

(In my practice, the above can actually be rolled back normally, if context1. SaveChanges() succeeded, context2. SaveChanges() is problematic, neither will be executed successfully!
Reference the using System.Transaction namespace in your project. Defining a TransactionScope in using is equivalent to defining a scope of things, i.e., the scope of this transaction is within using. In the using scope, if there is no scope. Complete() command, then the scope will automatically roll back all operations when destroyed. )

This may not necessarily meet our needs. If we need context1 and context2 to be executed successfully at the same time, or neither succeeds, we need to make small adjustments to the code, such as using the following code:



We use SaveChanges(false) to send the necessary database operation commands to the database first, which is to note that context1 and context2 have not really changed, if the transaction is terminated and automatically rolled back, neither change is actually committed to the database, so it can be successfully rolled back.




Previous:EF6 uses Database.BeginTransaction to manage transactions
Next:EF ORM Development and Use Series Tutorials
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com