Update Records in Entity Framework Core

Update Records in Entity Framework Core

The Entity Framework Core executes UPDATE statement in the database for the entities whose EntityState is Modified. The Database Context keeps tracks of all entities that have their EntityState value as modified.

I will use the DbContext.Update() method for updating entities.

Consider the below code:

var dept = new Department()
{
    Id = 1,
    Name = "Designing"
};

using (var context = new CompanyContext())
{
    context.Update(dept);
    await context.SaveChangesAsync();
}

The above code updates the Department name of the department with Id 1. It set’s its name to Designing.

The reason why EF Core does the update here is because the entity has a valid property key value (which is ‘1’ in this case). So it sets its EntityState to Modified.

Case when the .Update() will Insert a new record

If there is no valid property key value then EF core will set the EntityState to Added. The below given code does not have the Id property for Department entity, so on doing the update the EF core Inserts a new Department record on the database.

var dept = new Department()
{
    Name = "Research"
};
 
using (var context = new CompanyContext())
{
    context.Update(dept);
    await context.SaveChangesAsync();
}

Here the .Update() method will insert a new Research record on the Department table.

update method inserts a new record

Update Multiple Records

Use DbContext.UpdateRange() method to Update Multiple Records at the same time.

Consider the below code where I am updating 3 Department records at the same time.

var dept1 = new Department()
{
    Id = 1,
    Name = "New Designing"
};

var dept2 = new Department()
{
    Id = 2,
    Name = "New Research"
};

var dept3 = new Department()
{
    Id = 3,
    Name = "New HR"
};
 
List<Department> modifiedDept = new List<Department>() { dept1, dept2, dept3 };

using (var context = new CompanyContext())
{
    context.UpdateRange(modifiedDept);
    await context.SaveChangesAsync();
}
update multiple records
I have covered very advanced model binding in my article called Advanced Model Binding Concepts in ASP.NET Core

Update Related Records

The below code updates the Department name to ‘Admin_1’. This Department is the related record for the Employee Entity.

var dept = new Department()
{
    Id = 5,
    Name = "Admin_1"
};

var emp = new Employee()
{
    Id = 1,
    Name = "Matt_1",
    Designation = "Head_1",
    Department = dept
};

using (var context = new CompanyContext())
{
    context.Update(emp);
    await context.SaveChangesAsync();
}

When this code executes the employee having id as 1 gets his info changed to:

  • 1. Name is changed to “Matt_1”.
  • 2. Designation is changed to “Head_1”.

The related data which is the department also gets changed. This Employee Department name gets changed to “Admin_1”.

Download the source codes:

DOWNLOAD

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "ASP.NET Core, jQuery, EF Core, SEO, jQuery, HTML" and more.