Error Performing Update on Entity Framework Core

Asked

Viewed 332 times

0

I own a Teste de Integração that carries out the updating of an object in the database, for this I instate a new entity and insert the different properties and insert a Valid PK id however while performing the update is released the following exception:

{Microsoft.EntityFrameworkCore.Dbupdateconcurrencyexception: Database Operation expected to affect 1 Row(s) but Actually affected 0 Row(s). Data may have been modified or Deleted Since entities Were Loaded.

Due to the present time the object is not being Trackeado by EF for this I changed the mapper to work as follows:

using (var contexto = ContextUtils.ObtenhaContextSQLServer())
{
    using (var transacao = contexto.Database.BeginTransaction())
    {

        //Inseri para teste, Count == 0
        var antesDeAttach = contexto.Chamarizes.Local;

        //Inseri para teste
        contexto.Attach(model);

        //Inseri para teste, Count == 1
        var depoisDeAttach = contexto.Chamarizes.Local;

        contexto.Entry(model).State = EntityState.Modified;
        contexto.Entry(model).Property(x => x.DataCriacao).IsModified = false;

        contexto.Update(model);
        contexto.SaveChanges();
        transacao.Commit();
    }
}

I inserted a Attach of the entity model before performing the Update but the exception remains, to analyze the problem, I instructed the code with the entity verification Trackeadas and the result is that yes the entity is being. What would be the reason for the problem? and possible solution?

Versions: EF Core 2.1.1.0

  • I had this error in two cases: update without passing the primary key(id) or update when the record did not exist in the bd

1 answer

0

When you have a model that is disconnected from context (or was searched for by another context) Entity does not have the Tracking of this model. In this case the best means (or at least what I use and works well) is you to fetch the model again and do the update of modelAltered into the model, for this the Entity has the method SetValues().

See the example of my method Save (I made some changes to make it more readable):

    public virtual void Save(Pessoa p)
    {
        //----- Faz verificação se entidade existe no banco
        var pessoaExistente = contexto.Pessoa.Find(p.Id);

        if (pessoaExistente != null)
        {
            contexto.Entry(pessoaExistente ).CurrentValues.SetValues(p);
            contexto.Update(p);
        }
        else
        {
            contexto.Update(p);
        }

        contexto.SaveChanges();
    }

The method Update updates or inserts the object into the database, but you cannot (as far as I searched) pass the a context trackeado, he will try to insert.

I am still researching on the behavior since case to related entities, consider that you may have problems regarding this. Example, if the model Person has an entity City related to it, the Entity may try to enter that city or (in the case of Update) try to update it.

But I hope in general I can help you find a solution.

ps. This code is implemented in Efcore 2.2.4

Browser other questions tagged

You are not signed in. Login or sign up in order to post.