DbContext.SaveChangesAsync Method

Definition

Overloads

SaveChangesAsync(Boolean, CancellationToken)

Saves all changes made in this context to the database.

SaveChangesAsync(CancellationToken)

Saves all changes made in this context to the database.

SaveChangesAsync(Boolean, CancellationToken)

Saves all changes made in this context to the database.

public virtual System.Threading.Tasks.Task<int> SaveChangesAsync (bool acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken = default);
abstract member SaveChangesAsync : bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
override this.SaveChangesAsync : bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
Public Overridable Function SaveChangesAsync (acceptAllChangesOnSuccess As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Integer)

Parameters

acceptAllChangesOnSuccess
Boolean

Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

Exceptions

An error is encountered while saving to the database.

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Applies to

SaveChangesAsync(CancellationToken)

Saves all changes made in this context to the database.

public virtual System.Threading.Tasks.Task<int> SaveChangesAsync (System.Threading.CancellationToken cancellationToken = default);
abstract member SaveChangesAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
override this.SaveChangesAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
Public Overridable Function SaveChangesAsync (Optional cancellationToken As CancellationToken = Nothing) As Task(Of Integer)

Parameters

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

Exceptions

An error is encountered while saving to the database.

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Applies to