Fórum Erro ao executar o SaveChanges com EntityFramework Core #603895
24/07/2019
0
Estou com um problema ao executar o SaveChanges em um contexto.
Primeiro executo um Add e posteriormente um Update em um objeto recuperado anteriormente. Ao realizar essas ações ocorre o seguinte erro: The instance of entity type 'ProcessoOsOperacaoEntity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Abaixo segue o código:
public class ProcessoOsOperacaoEntity
{
public int? Id {get; set; }
public int? IdProcessoOs { get; set; }
public virtual ProcessoOsEntity ProcessoOs { get; set; }
public int? IdUsuario { get; set; }
public virtual UsuarioEntity Usuario { get; set; }
public int? IdPatrimonio { get; set; }
public virtual PatrimonioEntity Patrimonio { get; set; }
public DateTime Inicio { get; set; } = DateTime.Now;
public StatusOperacao? Status { get; set; }
public decimal? Qtde { get; set; } = 0;
public int? IdReinicio { get; set; }
public virtual ProcessoOsOperacaoEntity Reinicio { get; set; }
}
using (var rep = new DataContext())
{
var op = rep.Operacoes.Where(w => w.IdProcessoOs == _processoOs.Id &&
w.IdUsuario == _usuario.Id)
.OrderByDescending(od => od.Id)
.FirstOrDefault();
if (op != null) { if (op.Status != StatusOperacao.Pausa) { op = default; } }
var operacao = new ProcessoOsOperacaoEntity
{
IdProcessoOs = _processoOs?.Id,
ProcessoOs = _processoOs,
IdUsuario = _usuario?.Id,
Usuario = _usuario,
IdPatrimonio = _patrimonio?.Id,
Patrimonio = _patrimonio,
Inicio = DateTime.Now,
Status = StatusOperacao.Executando,
Qtde = 0
};
rep.Add(operacao);
if (op != null)
{
op.IdReinicio = operacao.Id;
op.Reinicio = operacao;
rep.Update(op);
}
rep.SaveChanges();
}
Primeiro executo um Add e posteriormente um Update em um objeto recuperado anteriormente. Ao realizar essas ações ocorre o seguinte erro: The instance of entity type 'ProcessoOsOperacaoEntity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Abaixo segue o código:
public class ProcessoOsOperacaoEntity
{
public int? Id {get; set; }
public int? IdProcessoOs { get; set; }
public virtual ProcessoOsEntity ProcessoOs { get; set; }
public int? IdUsuario { get; set; }
public virtual UsuarioEntity Usuario { get; set; }
public int? IdPatrimonio { get; set; }
public virtual PatrimonioEntity Patrimonio { get; set; }
public DateTime Inicio { get; set; } = DateTime.Now;
public StatusOperacao? Status { get; set; }
public decimal? Qtde { get; set; } = 0;
public int? IdReinicio { get; set; }
public virtual ProcessoOsOperacaoEntity Reinicio { get; set; }
}
using (var rep = new DataContext())
{
var op = rep.Operacoes.Where(w => w.IdProcessoOs == _processoOs.Id &&
w.IdUsuario == _usuario.Id)
.OrderByDescending(od => od.Id)
.FirstOrDefault();
if (op != null) { if (op.Status != StatusOperacao.Pausa) { op = default; } }
var operacao = new ProcessoOsOperacaoEntity
{
IdProcessoOs = _processoOs?.Id,
ProcessoOs = _processoOs,
IdUsuario = _usuario?.Id,
Usuario = _usuario,
IdPatrimonio = _patrimonio?.Id,
Patrimonio = _patrimonio,
Inicio = DateTime.Now,
Status = StatusOperacao.Executando,
Qtde = 0
};
rep.Add(operacao);
if (op != null)
{
op.IdReinicio = operacao.Id;
op.Reinicio = operacao;
rep.Update(op);
}
rep.SaveChanges();
}

Bruno Guelere
Curtir tópico
+ 0
Responder
Posts
25/07/2019
Renato Dias
Fala Bruno! Para cada operação (insert, update, delete) você tem que executar um savechanges antes de ir para a próxima.
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)