0
I’m having trouble updating a model, the code below is working correctly when I give GET in a context model edits on the screen (View) and I give a Submit everything ok but when I have a data grid where I load several messages and for example I will check (checkbox) on this grid to change an active flag to inactive it does not proceed but also not error ?
I think it has to do with Objectstatemanager in all actions that it performs correctly State is with Datached but when the does not update without giving any error State is as Unchanged.
A great note to help me is that this method always comes with my Model ID it is only used to Edit and to insert I have another method.
Follows the method
public void Edit(TEntity entity)
{
var entry = _context.Entry<TEntity>(entity);
var pkey = _dbSet.Create().GetType().GetProperty("ID").GetValue(entity);
if ((entry.State == EntityState.Detached))
{
var set = _context.Set<TEntity>();
TEntity attachedEntity = set.Find(pkey);
if (attachedEntity != null)
{
var attachedEntry = _context.Entry(attachedEntity);
attachedEntry.CurrentValues.SetValues(entity);
}
else
{
entry.State = EntityState.Modified;
}
}
try
{
_context.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
StringBuilder sb = new StringBuilder();
foreach (var failure in ex.EntityValidationErrors)
{
sb.AppendFormat("{0} Falha da validação ", failure.Entry.Entity.GetType());
foreach (var error in failure.ValidationErrors)
{
sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
sb.AppendLine();
}
}
throw new System.Data.Entity.Validation.DbEntityValidationException("Erros da validação da Entidade: " + sb.ToString(), ex);
}
}
Who can give me a way out so that always this method of an independent UPDATE if it comes with the State, I am grateful. there is one more thing I’ve been stirring and had situations where he gave error of existing Objectstatemanager too.
The problem is I believe that in View for Viewmodel... Do you use WPF? MVVM?
– Danilo Breda
Yes it is an MVC5 MVVM application, strange that I will try to apply the Automapper because in case it is not updating, I made a Modelview to adquar what I needed in the View screen things understands with grid tals when I give a form Submit does not actualize and not the error rrsrs I will integrate the automapper make my MV talk correctly with Mode DTO, brief post here what is not yet discovered.
– Rodrigo