Lets say I have an entity Book and a DbSet called Books in my DbContext.
The Book entity looks like this:
class Book
{
int Id { get; set; } //Id is the primary key
string Title { get; set; }
int Pages { get; set; }
string Author { get; set; }
}
Now let's say I want to update a book that already exists in my context:
var editingBook = async _context.Books.FindAsync(100);
// For simplicity's sake lets say that a Book with Id 100 exists and was retrieved
Is there a difference between updating the book in any of the following ways?
editingBook.title = 'New Title'
await _context.SaveAsync()
editingBook.title = 'New Title'
_context.Books.Update(editingBook);
await _context.SaveAsync();
It is my current assumption that the call for update would only be needed if we lost context somehow, for example if we got the book .AsNoTracking() or if we copied the values from editingBook into a new Book. In that case does Update try to find an existing book with the same primary ID to update in context?
Is there functionally a difference between using Update vs just Saving the current context when you still have tracking?
Edit: I understand that both of these work. I am not looking for instructions on how to update an entity in EF. I am looking for a peek into the functional difference of these two methods explained by someone with deep knowledge of EF and its inner workings.