diff --git a/src/Extensions/DataServices/BootstrapBlazor.DataAccess.EntityFrameworkCore/DefaultDataService.cs b/src/Extensions/DataServices/BootstrapBlazor.DataAccess.EntityFrameworkCore/DefaultDataService.cs index 79ccc1a9e0b..f049544b479 100644 --- a/src/Extensions/DataServices/BootstrapBlazor.DataAccess.EntityFrameworkCore/DefaultDataService.cs +++ b/src/Extensions/DataServices/BootstrapBlazor.DataAccess.EntityFrameworkCore/DefaultDataService.cs @@ -59,6 +59,14 @@ public Task CancelAsync() public Task EditAsync(object model) { Model = model as TModel; + if (Model != null) + { + var existingEntity = _db.Set().Local.FirstOrDefault(e => e == Model); + if (existingEntity != null) + { + _db.Entry(existingEntity).State = EntityState.Detached; + } + } return Task.CompletedTask; } @@ -92,6 +100,10 @@ public override async Task SaveAsync(TModel model, ItemChangedType changed } await _db.SaveChangesAsync(); + + // 保存完成后,分离实体,避免进一步跟踪 + _db.Entry(model).State = EntityState.Detached; + return true; }