fix agent summary update
This commit is contained in:
@@ -125,15 +125,28 @@ public class AgentSummaryRepository : IAgentSummaryRepository
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Update existing record - attach and modify the entity
|
// Update existing record - check if already tracked first
|
||||||
var entityToUpdate = MapToEntity(agentSummary);
|
var entityToUpdate = MapToEntity(agentSummary);
|
||||||
entityToUpdate.Id = existing.Id; // Preserve the existing ID
|
entityToUpdate.Id = existing.Id; // Preserve the existing ID
|
||||||
entityToUpdate.CreatedAt = existing.CreatedAt; // Preserve creation date
|
entityToUpdate.CreatedAt = existing.CreatedAt; // Preserve creation date
|
||||||
entityToUpdate.UpdatedAt = DateTime.UtcNow;
|
entityToUpdate.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
// Attach the entity and mark it as modified
|
// Check if an entity with this key is already being tracked
|
||||||
_context.AgentSummaries.Attach(entityToUpdate);
|
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
||||||
_context.Entry(entityToUpdate).State = EntityState.Modified;
|
.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();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user