0

I have an Entity Data Model that is made up of these multiple entities - Vendor, Payment, VendorContact, Invoice and GLAccount. I am using EF version 4.

In some existing code there are 5 methods being called one after the other, and these are saving changes for the above entity tables in database. The methods are as described below.

  1. Method1 changes data for a certain Vendor object in memory and then calls SaveChanges method on object context.
  2. Method2 changes data for Payments objects and calls SaveChanges.
  3. Method3 changes data for VendorContacts objects and calls SaveChanges.
  4. Method4 changes data for Invoices objects and calls SaveChanges.
  5. Method5 changes data for GLAccounts objects and calls SaveChanges.

My question is: Would I gain much in performance if I called SaveChanges method only once Vs calling it five times? Right now, Method1 thru to Method5 are taking about 25 seconds to complete.

So my proposal is to remove SaveChanges from all the methods and call SaveChanges only after Method5 call is done.

1 Answer 1

1

I think the best answer here would be: try, benchmark your attempt and then come to a conclusion. It sounds logical that it might be faster to make one commit to the DB instead of 5, but then you deal with more data. It might also vary by the environment you're testing on. I say go ahead and give it a try, then pist the result so we can all benefit :)

Edit

Found some benchmarks related to your question: When should I call SaveChanges() when creating 1000's of Entity Framework objects? (like during an import)

Sign up to request clarification or add additional context in comments.

2 Comments

My problem is I can only try after my proposal for code changes are accepted by the owner of the app in our company. So I was right now looking for some answers based on what some may have experienced. Of course, if my proposal is accepted I will let you guys know of my results. They will not allow me to check out the code from source control till the proposal is accepted.
That link is helpful. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.