Entity framework 5 batch addition, deletion, modification efficiency optimization, entityframework
The slowest time for batch addition, deletion, and modification of data is to commit a transaction after one operation.
The following is an optimization test for the addition, deletion, and modification operations:
Same 300 data records
Batch Add a transaction only submitted once
Time in use: 10673.5444 ms
Batch Add a transaction only submitted once and set context. Configuration. AutoDetectChangesEnabled = false
Time in use: 5284.5425 ms
Submit transactions only once for batch Modification
Time in use: 3472.8314 ms
Submit the transaction only once for batch modification and set context. Configuration. AutoDetectChangesEnabled = false
Time in use: 1993.7855 ms
Batch deletion only submits transactions once
Time in use: 5961.3264 ms
Batch Delete only commit transactions once and set context. Configuration. AutoDetectChangesEnabled = false
Time in use: 1346.8273 ms
A special reminder is that my deletion method uses a replacement to delete data, instead of checking data from the database before deleting it.
Comparison of the two deletion Methods
Slow
Var stu = context. Students. SingleOrDefault (s => s. StudentNo = 23230 );
Context. Students. Remove (stu );
Context. SaveChanges ();
Fast
Var stu = new Student {StudentNo = 23230 };
Context. Students. Attach (stu );
Context. Students. Remove (stu );
Context. SaveChanges ();
Obviously, the efficiency improvement is obvious.