Skip to content

Commit

Permalink
fix(EFCore): update EditAsync method (#4249)
Browse files Browse the repository at this point in the history
Co-authored-by: Argo Zhang <argo@live.ca>
  • Loading branch information
luodaoyi and ArgoZhang authored Sep 10, 2024
1 parent 29e1d10 commit f118a21
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public Task CancelAsync()
public Task EditAsync(object model)
{
Model = model as TModel;
if (Model != null)
{
var existingEntity = _db.Set<TModel>().Local.FirstOrDefault(e => e == Model);
if (existingEntity != null)
{
_db.Entry(existingEntity).State = EntityState.Detached;
}
}
return Task.CompletedTask;
}

Expand Down Expand Up @@ -92,6 +100,10 @@ public override async Task<bool> SaveAsync(TModel model, ItemChangedType changed
}

await _db.SaveChangesAsync();

// 保存完成后,分离实体,避免进一步跟踪
_db.Entry(model).State = EntityState.Detached;

return true;
}

Expand Down

0 comments on commit f118a21

Please sign in to comment.