[英].Net Core error System.InvalidOperationException: The instance of entity type is already being tracked
i´m developing a web api with angular and.Net Core.我正在使用 angular 和.Net Core 开发 web api。 I'm really new in.Net, but when i try to update a process at this endpoint, i get this error: "An unhandled exception has occurred while executing the request. System.InvalidOperationException: The instance of entity type 'EtapaEmpleado' cannot be tracked because another instance with the same key value for {'IdEtapa', 'IdEmpleado'} is already being tracked".我真的是.Net的新手,但是当我尝试在此端点更新进程时,出现此错误:“执行请求时发生未处理的异常。System.InvalidOperationException:实体类型'EtapaEmpleado'的实例不能被跟踪,因为另一个具有相同键值的实例 {'IdEtapa', 'IdEmpleado'} 已经被跟踪”。
This is my code:这是我的代码:
[HttpPut("{id}")]
public async Task<IActionResult> PutEtapa([FromRoute] int id, [FromBody] EditEtapaDto editEtapaDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != editEtapaDto.IdEtapa)
{
return BadRequest();
}
var preEtapa = _mapper.Map<Etapa>(editEtapaDto);
//Prueba
foreach(var a in preEtapa.EtapaEmpleado)
{
var preEtapaEmpleado=_mapper.Map<EtapaEmpleado>(a);
if(_context.EtapaEmpleado.Where(z=>z.IdEmpleado==preEtapaEmpleado.IdEmpleado).Count()>0 && _context.EtapaEmpleado.Where(x=>x.IdEtapa==preEtapaEmpleado.IdEtapa).Count()>0)
{
var tiempoAntes=_context.EtapaEmpleado.FirstOrDefault(z=>z.IdEmpleado==preEtapaEmpleado.IdEmpleado && z.IdEtapa==preEtapaEmpleado.IdEtapa);
var condicion=String.IsNullOrEmpty(tiempoAntes.TiempoParc);
if(condicion==false)
{
string[] arrTAntesStr=tiempoAntes.TiempoParc.Split(':');
string[] arrTDespuesStr=preEtapaEmpleado.TiempoParc.Split(':');
int[] tiempoAntesArrInt= Array.ConvertAll(arrTAntesStr,Int32.Parse);
int[] tiempoDespuesArrInt=Array.ConvertAll(arrTDespuesStr,Int32.Parse);
int[] tiempoIntFinal={0,0,0,0};
int meLlevoUno=0;
string tiempoStringFinal=null;
for(int i=3;i>-1;i--)
{
switch(i){
default:
tiempoIntFinal[i]=tiempoAntesArrInt[i]+tiempoDespuesArrInt[i]+meLlevoUno;
meLlevoUno=tiempoIntFinal[i]/60;
tiempoIntFinal[i]=tiempoIntFinal[i]%60;
break;
case 0:
tiempoIntFinal[i]=tiempoAntesArrInt[i]+tiempoDespuesArrInt[i]+meLlevoUno;
break;
case 1:
tiempoIntFinal[i]=tiempoAntesArrInt[i]+tiempoDespuesArrInt[i]+meLlevoUno;
meLlevoUno=tiempoIntFinal[i]/24;
tiempoIntFinal[i]=tiempoIntFinal[i]%24;
break;
}
}
var builder = new StringBuilder();
for(int j=0;j<tiempoIntFinal.Length;j++)
{
if(tiempoIntFinal[j]<10)
{
builder.Append("0");
}
builder.Append(tiempoIntFinal[j]);
if(j!=3)
{
builder.Append(":");
}
tiempoStringFinal=builder.ToString();
}
preEtapaEmpleado.TiempoParc=tiempoStringFinal;
}
try{
//preEtapaEmpleado.TiempoParc=preEtapa.TiempoParc;
_repo2.Update(preEtapaEmpleado);
}
catch(DbUpdateConcurrencyException){
throw;
}
}
else{
try{
_repo2.Add(preEtapaEmpleado);
}
catch(DbUpdateConcurrencyException)
{
throw;
}
}
await _repo2.SaveAsync(preEtapaEmpleado);
}
_repo.Update(preEtapa);
await _repo.SaveAsync(preEtapa);
return StatusCode(201,preEtapa);
And this is my IDataRepository:这是我的 IDataRepository:
namespace Foha.Repositories
{
public interface IDataRepository<T> where T : class
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
Task<T> SaveAsync(T entity);
}
}
When you call你打电话时
var tiempoAntes=_context.EtapaEmpleado.FirstOrDefault(z=>z.IdEmpleado==preEtapaEmpleado.IdEmpleado && z.IdEtapa==preEtapaEmpleado.IdEtapa);
It starts tracking that entity in _context
.它开始在_context
中跟踪该实体。 I assume that _repo2
use the same _context
?我假设_repo2
使用相同的_context
?
If that is true, then when you try to add a copy of the object _repo2.Update(preEtapaEmpleado);
如果这是真的,那么当您尝试添加 object _repo2.Update(preEtapaEmpleado);
的副本时with the same id, the context recognizes it as a duplicate and throws the error.使用相同的 id,上下文将其识别为重复并引发错误。
This article might help这篇文章可能会有所帮助
https://docs.microsoft.com/en-us/ef/core/querying/tracking https://docs.microsoft.com/en-us/ef/core/querying/tracking
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.