DbContext.SaveChanges Method

Definition

Saves all changes made in this context to the underlying database.

public virtual int SaveChanges ();
abstract member SaveChanges : unit -> int
override this.SaveChanges : unit -> int
Public Overridable Function SaveChanges () As Integer

Returns

The number of state entries written to the underlying database. This can include state entries for entities and/or relationships. Relationship state entries are created for many-to-many relationships and relationships where there is no foreign key property included in the entity class (often referred to as independent associations).

Exceptions

An error occurred sending updates to the database.

A database command did not affect the expected number of rows. This usually indicates an optimistic concurrency violation; that is, a row has been changed in the database since it was queried.

The save was aborted because validation of entity property values failed.

An attempt was made to use unsupported behavior such as executing multiple asynchronous commands concurrently on the same context instance.

The context or connection have been disposed.

Some error occurred attempting to process entities in the context either before or after sending commands to the database.

Applies to