AcceptAllChanges and SaveChanges methods in Entity Framework
The default behavior of SaveChanges() method is when a transaction is executed successfully, it calls DetectChangesBeforeSave() and AcceptChangesafterSave() methods automatically.
This automatic calling happens only if we have not passed any values to the SaveChanges() method.
The statement ctx.SaveChanges(); is equivalent to ctx.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptChangesAfterSave);
The SaveChanges() method takes an enum of type SaveOptions. The enum can have three possible values. Either we can send a single value to this method or we can combine the values and send as shown above. The possible ways we can call the SaveChanges() method are:
1.ctx.SaveChanges(AcceptChangesafterSave); - The AcceptAllChanges method is invoked after persistence happens to the database (INSERT and UPDATE commands are executed depending on the state of entities) and all the entities which are in added and modified states are changed to UnChanged state. Because the AcceptAllChanges() method resets the object state to Unchanged, if we want to explicitly call this function, we should call it only after entities persist (after data is saved). Otherwise changes are not saved to the database.
2.ctx.SaveChanges(DetectChangesBeforeSave); - calls the DetectChanges method before saving changes and synchronization happens between entities and ObjetctStateManager entries. In the article, Snapshot Change Tracking using DetectChanges(), importance of calling DetectChanges when we are using POCOs is explained.
ctx.DeleteObject(selectedcustomer); ctx.SaveChanges(SaveOptions.DetectChangesBeforeSave);
3.ctx.SaveChanges(None); neither DetectChanges nor AcceptAllChanges methods are called.
Understanding AcceptAllChanges and SaveChanges in a transaction
EF maintains transactions implicitly. When we call SaveChanges() method, the ObjectContext starts the transaction and commits the changes. If any operation fails, rollback occurs. Consider the following example. If the default transaction is successful (entities are persisted successfully), both the DetectChanges and AcceptAllChanges methods are called. However, we can change this default behavior by passing SaveOptions enum to SaveChanges() as we have seen earlier in this article.
The statement ctx.SaveChanges(); is equivalent to ctx.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptChangesAfterSave);
The SaveChanges() method takes an enum of type SaveOptions. The enum can have three possible values. Either we can send a single value to this method or we can combine the values and send as shown above. The possible ways we can call the SaveChanges() method are:
1.ctx.SaveChanges(AcceptChangesafterSave); - The AcceptAllChanges method is invoked after persistence happens to the database (INSERT and UPDATE commands are executed depending on the state of entities) and all the entities which are in added and modified states are changed to UnChanged state. Because the AcceptAllChanges() method resets the object state to Unchanged, if we want to explicitly call this function, we should call it only after entities persist (after data is saved). Otherwise changes are not saved to the database.
2.ctx.SaveChanges(DetectChangesBeforeSave); - calls the DetectChanges method before saving changes and synchronization happens between entities and ObjetctStateManager entries. In the article, Snapshot Change Tracking using DetectChanges(), importance of calling DetectChanges when we are using POCOs is explained.
ctx.DeleteObject(selectedcustomer); ctx.SaveChanges(SaveOptions.DetectChangesBeforeSave);
3.ctx.SaveChanges(None); neither DetectChanges nor AcceptAllChanges methods are called.
Understanding AcceptAllChanges and SaveChanges in a transaction
EF maintains transactions implicitly. When we call SaveChanges() method, the ObjectContext starts the transaction and commits the changes. If any operation fails, rollback occurs. Consider the following example. If the default transaction is successful (entities are persisted successfully), both the DetectChanges and AcceptAllChanges methods are called. However, we can change this default behavior by passing SaveOptions enum to SaveChanges() as we have seen earlier in this article.
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- Rules to be followed while creating POCOs - 4 important rules
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- How to load related POCO entities - loading patterns and their differences in their usage with examples
- How to perform Lazy Loading with POCO classes
- What is Change tracking in POCOs, ways of tracking
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Entity Framework EntityKey object
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- ObjectStateEntry class
- Entity state in Entity Framework - what, how, why?
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net