fix agent summary update

This commit is contained in:
2025-08-15 21:49:27 +07:00
parent 513f880243
commit ece75b1973

View File

@@ -125,15 +125,28 @@ public class AgentSummaryRepository : IAgentSummaryRepository
}
else
{
// Update existing record - attach and modify the entity
// Update existing record - check if already tracked first
var entityToUpdate = MapToEntity(agentSummary);
entityToUpdate.Id = existing.Id; // Preserve the existing ID
entityToUpdate.CreatedAt = existing.CreatedAt; // Preserve creation date
entityToUpdate.UpdatedAt = DateTime.UtcNow;
// Attach the entity and mark it as modified
_context.AgentSummaries.Attach(entityToUpdate);
_context.Entry(entityToUpdate).State = EntityState.Modified;
// Check if an entity with this key is already being tracked
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
.FirstOrDefault(e => e.Entity.Id == existing.Id);
if (trackedEntity != null)
{
// Entity is already tracked, update its values
MapToEntity(agentSummary, trackedEntity.Entity);
trackedEntity.Entity.UpdatedAt = DateTime.UtcNow;
trackedEntity.State = EntityState.Modified;
}
else
{
// Entity is not tracked, use Update method which handles state properly
_context.AgentSummaries.Update(entityToUpdate);
}
await _context.SaveChangesAsync();