I'm using a repository patter for my webApi, and trying to make Update work. What this does is receives a "new" entity from the mvc webapi controller, and tries to update the existing object
public void Update(TEntity entity)
{
var oldEntry = _context.Set<TEntity>().Find(entity.Id);
oldEntry = Mapper.Map(entity, oldEntry);
Console.WriteLine(oldEntry.ToString());
_context.SaveChanges();
}
Save changes does nothing.
If I explicitly specify
oldEntry.SomeTextProperty = "TestText";
and then call
_context.SaveChanges();
all is good. How to fix this ? or work around this ? Maybe there is a way to tell AutoMapper to invoke property setters ?
You have to tell EF that you modified your entity before saving changes. This should work in framework 4.5
_context.Set<TEntity>().AddOrUpdate(new[] {oldEntry});
Or maybe this in framework 4:
_context.Entry(oldEntry).State = System.Data.EntityState.Modified;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With