EF Core Save Data
Updating data using EF Core is straightforward. Retrieve the entity, modify its properties, and call `SaveChanges()`. EF Core automatically generates the appropriate SQL statements to update the corresponding record in the database, making data updates a breeze.
To only update one field, we can simply change the update method to the following:
Person person = new Person {Id=4, Lastname="Miller"};
dbContext.Attach(person);
dbContext.Entry(person).Property(p => p.Lastname).IsModified = true;
dbContext.SaveChanges();
The above function first constructs the object with the specified Id and updated Lastname, and then appends the object; it then explicitly marks the Lastname property as modified.
The generated UPDATE statement now looks like this
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[@p1='?' (DbType = Int32), @p0='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
UPDATE `Persons` SET `Lastname` = @p0
WHERE `Id` = @p1;
SELECT ROW_COUNT();
As shown in the EFCore log, only the Lastname field is updated.
This approach can slightly improve performance because the SQL statement is smaller and the query execution on the database server can be faster.
See also the discussion at Stack Overflow How to update not every fields of an object using Entity Framework and EntityState.Modified
Make EF Core update only the updated properties by turning the disconnected scenario to connected.
public void SaveBook(Book book)
{
// Here, 'book' is the book with the changed Title.
using(var context = new TestContext())
{
var dbBook = context.Books.Find(book.ID);
// Copy book's property values to dbBook.
context.Entry(dbBook).CurrentValues.SetValues(book);
context.SaveChanges();
}
}
There may be good reasons to prefer the latter method above the former.
This code sample show how to insert an entity Element without updating related entities.
_dbContext.Entry<Element>(element).State = EntityState.Added;
// Set the state of the Category navigation property of 'element' to Unchanged
_dbContext.Entry<Category>(element.Category).State = EntityState.Unchanged;
// Detach all roles associated with the 'element' from the DbContext
element.Roles.ToList().ForEach(r => _dbContext.Entry<Role>(r).State = EntityState.Detached);
// Mark the Roles collection of 'element' as not modified to prevent updating roles
_dbContext.Entry<Element>(element).Collection(r => r.Roles).IsModified = false;
await _dbContext.SaveChangesAsync();
- The
element's associatedCategoryobject to Unchanged in the_dbContext. This indicates that theCategoryobject is not modified and should not be updated in the database. - For each
Role, it sets the state toDetached. Detaching an entity means it is no longer tracked by the EF Core context for changes. - The
IsModifiedproperty of theRolescollection in theelementis explicitly set tofalse. This indicates to EF Core that theRolescollection has not been modified and should not be updated in the database.
Comments