Persists all updates to the data source and optionally resets change tracking in the object context.
Namespace:
System.Data.Objects
Assembly:
System.Data.Entity (in System.Data.Entity.dll)
Visual Basic (Declaration)
Public Function SaveChanges ( _
acceptChangesDuringSave As Boolean _
) As Integer
Dim instance As ObjectContext
Dim acceptChangesDuringSave As Boolean
Dim returnValue As Integer
returnValue = instance.SaveChanges(acceptChangesDuringSave)
public int SaveChanges(
bool acceptChangesDuringSave
)
public:
int SaveChanges(
bool acceptChangesDuringSave
)
public function SaveChanges(
acceptChangesDuringSave : boolean
) : int
Parameters
- acceptChangesDuringSave
- Type: System..::.Boolean
This parameter is needed for client-side transaction support. If true, the change tracking on all objects is reset after SaveChanges(Boolean) finishes. If false, you must call the AcceptAllChanges method after SaveChanges(Boolean).
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. The SaveChanges method 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 values, and then calling the SaveChanges method 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. The example tries to save changes, which may cause a concurrency conflict. Then it shows how to resolve the concurrency conflict by refreshing the object context before re-saving changes.
Using context As New AdventureWorksEntities()
Try
' Perform an operation with a high-level of concurrency.
' Change the status of all orders without an approval code.
Dim orders As ObjectQuery(Of SalesOrderHeader) = _
context.SalesOrderHeader.Where( _
"it.CreditCardApprovalCode IS NULL").Top("100")
For Each order As SalesOrderHeader In orders
' Reset the order status to 4 = Rejected.
order.Status = 4
Next
Try
' Try to save changes, which may cause a conflict.
Dim num As Integer = context.SaveChanges()
Console.WriteLine("No conflicts. " + _
num.ToString() + " updates saved.")
Catch ex As OptimisticConcurrencyException
' Resolve the concurrency conflict by refreshing the
' object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders)
' Save changes.
context.SaveChanges()
Console.WriteLine("OptimisticConcurrencyException " _
+ "handled and changes saved.")
End Try
For Each order As SalesOrderHeader In orders
Console.WriteLine("Order ID: " + order.SalesOrderID.ToString() _
+ " Order status: " + order.Status.ToString())
Next
Catch ex As UpdateException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Perform an operation with a high-level of concurrency.
// Change the status of all orders without an approval code.
ObjectQuery<SalesOrderHeader> orders =
context.SalesOrderHeader.Where(
"it.CreditCardApprovalCode IS NULL").Top("100");
foreach (SalesOrderHeader order in orders)
{
// Reset the order status to 4 = Rejected.
order.Status = 4;
}
try
{
// Try to save changes, which may cause a conflict.
int num = context.SaveChanges();
Console.WriteLine("No conflicts. " +
num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders);
// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}
foreach (SalesOrderHeader order in orders)
{
Console.WriteLine("Order ID: " + order.SalesOrderID.ToString()
+ " Order status: " + order.Status.ToString());
}
}
catch (UpdateException ex)
{
Console.WriteLine(ex.ToString());
}
}
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.
.NET Framework
Supported in: 3.5 SP1
Reference
Other Resources