Updating a single record

I am using the Entity Framework 6 tutorials.

I have a method called ChangeDate (for example)

public void ChangeDate (Customer c)
{
    c.DateModidifed = datetime.Now();
}

In my unit of Work i call this method as

_uow.Customers.ChangeDate(myCustomer);
_uow.Save();

The changes never are saved back to the database.

If i change it to

public void ChangeDate (Customer c)
{
    c = Get(c.Id);
    c.DateModidifed = datetime.Now();
}

Then it saves the record back to the database?
Is this correct in how it should work? Even though i have passed in the record i want to change?

Is myCustomer a tracked entity? How did you retrieve it?

I don’t know how i can confirm its a tracked entity but would like a bit more info on that?

The page loads with a table using GetAll and displays the data for each customer. The user then clicks the row button which passes in the customer into the function.

To give a bit more info. This is in an AngularJS page. The object is created in AngularJS and it just gets passed in. In debug mode i can see Customer c with values filled in for the properties i need.

EF can only track changes of entities it knows about. If you newed up the customer instance yourself or had it deserialized from JSON (as I believe you had) EF doesn’t even know that that instance exists.

In your second example you retrieve the instance from EF so it knows about it, tracks changes to it and persists them when calling SaveChanges() on the DbContext.

AFAIK you can Attach() an entity to a DbSet, but I have never used that myself.

So to ensure i have understood you correctly:

In this scenario i do have an object in AngularJS which mimics Customer as it is in the DB (excluding a couple of properties). I do a GetAll using the UoW pattern and display the customers in a table.

When the user clicks the button against that row it passes in the object into an AngularJS function which in turn calls a webservice i have built.

So in this scenario - it would be correct to call a single customer again? i.e.

c = Get(c.Id);

I still dont understand how EF can track Entities it knows about? Is there a simple example you could provide? I’m trying to determine if my approach needs a tweak or not

If EF gives you an entity, it can keep a copy of the original state and a reference to the instance it returned to you. On SaveChanges() it can compare the original state with the current state of the instance (simplified of course).

This is how Mosh implemented a controller to update a vehicle entity (from the Build A Real World App With ASP.NET core and Angular course):

    [HttpPut("{id}")]
    public async Task<IActionResult> UpdateVehicle(int id, [FromBody] SaveVehicleResource vehicleResource)
    {
      if (!ModelState.IsValid)
        return BadRequest(ModelState);

      var vehicle = await repository.GetVehicle(id);

      if (vehicle == null)
        return NotFound();

      mapper.Map<SaveVehicleResource, Vehicle>(vehicleResource, vehicle);
      vehicle.LastUpdate = DateTime.Now;

      await unitOfWork.CompleteAsync();

      vehicle = await repository.GetVehicle(vehicle.Id);
      var result = mapper.Map<Vehicle, VehicleResource>(vehicle);

      return Ok(result);
    }

You can see that he first retrieves the current version of the vehicle from EF (so vehicle is tracked by EF) and then updates its properties with the properties of the vehicleResource send by the Angular app (that’s what mapper.Map() does).

1 Like