I am using Entity Framework 5 with the a DbContext.
In my scenario I am trying to update an existing entity and persist the changes to the database. Amongst other properties, the entity has a ‘LastUpdated’ property which is a DateTime.
My entities are POCO entities and my context inherits from DbContext.
Here is my code:
public void Update(Location location)
{
using (var context = new EfContext())
{
context.Locations.Attach(location);
context.Entry(location).State = EntityState.Modified;
context.SaveChanges();
}
}
The requirements of my application require me to attach the modified entity to a new context rather than retrieve the entity from the same context first.
This actually does work for all fields on the entity, except the LastUpdated field. It is a datetime and is not saving.
Is there something I am doing wrong here?
Try to pass the location LastUpdated property value as parameter in Update method