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
– Aesir