Entity Framework transaction handling via SaveChanges

Possibly save 1 hour of your time: A common question that was asked is how to handle transaction within your application.
Fortunately, if you are using Entity Framework, it already does the heavy lifting work for you.
When you modify your entities in the application, it will track model and property changes behind the scene. And then when you call the following line of code, it will persists all updates to the database and resets change tracking in the object context.


ObjectContext is the object that represents the database connection and its entities.

There may be situations when you may call this more than once in a request to create multiple transactions. I cannot think of a reason right now, but just remember that this acts like a commit transaction as each SaveChanges call will create a new transaction.

However, there are also cases where you may have more than one context to deal with. In that case you may pull data from one database and save them to another database. In this case, you may want to put both contexts’ SaveChanges calls in a TransactionScope to protect this as one atomic transaction.


ObjectContext.SaveChanges Method ()



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s