DbContext.SaveChanges() is one of two techniques for saving changes to the database with EF. With this method, you perform one or more tracked changes (add, update, delete), and then apply those changes by calling the SaveChanges method. As an alternative, ExecuteUpdate and ExecuteDelete can be used without involving the change tracker. For an introductory comparison of these two techniques, see the Overview page on saving data.
using (var context = new BloggingContext())
{
var blog = new Blog { Url = "http://example.com" };
context.Blogs.Add(blog);
context.SaveChanges();
}
Tip
The Add, Attach, and Update methods all work on the full graph of entities passed to them, as described in the Related Data section. Alternately, the EntityEntry.State property can be used to set the state of just a single entity. For example, context.Entry(blog).State = EntityState.Modified.
Updating Data
EF automatically detects changes made to an existing entity that is tracked by the context. This includes entities that you load/query from the database, and entities that were previously added and saved to the database.
Simply modify the values assigned to properties and then call SaveChanges:
using (var context = new BloggingContext())
{
var blog = context.Blogs.Single(b => b.Url == "http://example.com");
blog.Url = "http://example.com/blog";
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var blog = context.Blogs.Single(b => b.Url == "http://example.com/blog");
context.Blogs.Remove(blog);
context.SaveChanges();
}
If the entity already exists in the database, it will be deleted during SaveChanges. If the entity has not yet been saved to the database (that is, it is tracked as added) then it will be removed from the context and will no longer be inserted when SaveChanges is called.
Multiple Operations in a single SaveChanges
You can combine multiple Add/Update/Remove operations into a single call to SaveChanges:
using (var context = new BloggingContext())
{
// seeding database
context.Blogs.Add(new Blog { Url = "http://example.com/blog" });
context.Blogs.Add(new Blog { Url = "http://example.com/another_blog" });
context.SaveChanges();
}
using (var context = new BloggingContext())
{
// add
context.Blogs.Add(new Blog { Url = "http://example.com/blog_one" });
context.Blogs.Add(new Blog { Url = "http://example.com/blog_two" });
// update
var firstBlog = context.Blogs.First();
firstBlog.Url = "";
// remove
var lastBlog = context.Blogs.OrderBy(e => e.BlogId).Last();
context.Blogs.Remove(lastBlog);
context.SaveChanges();
}
Note
For most database providers, SaveChanges is transactional. This means all the operations either succeed or fail and the operations are never be left partially applied.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
This module guides you through the steps to create a data access project. You connect to a relational database and construct create, read, update, and delete (CRUD) queries by using Entity Framework Core (EF Core).