SaveChanges method of ObjectContext is a gateway to persist all changes made to entities to the database. When you call ObjectContext.SaveChanges(), it performs insert, update or delete operation on the database based on EntityState of the entities.
Following code shows how you can persist modification made to the Student entities of SchoolDB EDM created either with EntityObject entities or POCO Proxy entities.
As you can see in above code, we fetch the single Student entity whose name is “Student1” and then we change the StudentName property to “Student2”. It saves this modification to the database when we do ctx.SaveChanges(). This method also returns the number of rows updated in the database.
SaveChanges also accepts SaveOptions parameter. SaveOption is an Enum which has three values:
So this way SaveChanges method is the most important method in the EntityFramework.
Remember: SaveChanges method persist modifications made to all entities attached to it. So for example in above code, if you fetch and modify ‘StudentAddress’ entity also and call ctx.SaveChanges() then it will save modification of Student and StudentAddress entities to the database.