Three alternatives. Use the specific version instead of the object:
zyon.Entry<TB_Cliente>( client ).State = System.Data.Entity.EntityState.Modified;
Go straight on ChangeTracker
make the change:
zyon.ChangeTracker.Entries<TB_Cliente>().First( x => x.Entity == client ).State = System.Data.Entity.EntityState.Modified;
Or, if it's a particular code snippet, and you're already tired of the somewhat mysterious errors of EF, do both:
zyon.TB_Cliente.Attach( client );
zyon.Entry( client ).State = System.Data.Entity.EntityState.Modified;
zyon.ChangeTracker.Entries<TB_Cliente>().First( x => x.Entity == client ).State = System.Data.Entity.EntityState.Modified;
zyon.SaveChanges();
By documentation , DbContext.Entry(object)
should be a valid way to access the properties of a crawled or attached object, and changing the State to Modified would be all that is required to force the UPDATE of the data. But every now and then it does not just roll.
The generic version, if you want to do UPDATE on several objects in an immediate way (and outside the official transaction!):
public void DetachedUpdate(T obj)
{
using (var context = new Context())
{
context.Set<T>().Attach(obj);
context.ChangeTracker.Entries<T>().First(e => e.Entity == obj).State = EntityState.Modified;
context.SaveChanges();
}
}