ObjectContext::SaveChanges Method
Persists all updates to the data source and resets change tracking in the object context.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
Return Value
Type: System::Int32The number of objects in an Added, Modified, or Deleted state when SaveChanges was called.
| Exception | Condition |
|---|---|
| InvalidOperationException | The service adapter is nullptr. |
| OptimisticConcurrencyException | An optimistic concurrency violation has occurred in the data source. |
This method is to the same as calling SaveChanges(Boolean) and passing a value of true.
To ensure that objects on the client have been updated by data source-side logic, you can call the Refresh method with the StoreWins value after you call SaveChanges. For more information, see Saving Changes and Managing Concurrency (Entity Framework).
SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.
If an optimistic concurrency violation has occurred, an OptimisticConcurrencyException is thrown. You can resolve an optimistic concurrency violation by catching it, calling the Refresh method with the StoreWins or ClientWins value, and then calling SaveChanges again. For more information, see How to: Manage Data Concurrency in the Object Context (Entity Framework).
This example is based on the Adventure Works Sales Model. This example tries to save changes, which may cause a concurrency conflict. Then, it demonstrates how to resolve the concurrency conflict by refreshing the object context before re-saving changes.
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.