Refactor event, student, and team deletion logic to improve tracking and error handling
This commit updates the deletion logic in the Index.razor components for Events, Students, and Teams to load the entities fresh from the database with tracking. This change prevents tracking conflicts and ensures that the correct entity is deleted. Additionally, it adds error handling to notify users if the entity is not found or has already been deleted, enhancing the user experience and reliability of the deletion process.
This commit is contained in:
@@ -143,12 +143,27 @@
|
||||
|
||||
if (result == true)
|
||||
{
|
||||
Context.Events.Remove(evt!);
|
||||
// Load the event fresh from database with tracking to avoid tracking conflicts
|
||||
var eventToDelete = await Context.Events
|
||||
.FirstOrDefaultAsync(e => e.Id == evt.Id, cancellationToken);
|
||||
|
||||
if (_isDisposed) return;
|
||||
|
||||
if (eventToDelete == null)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
Snackbar.Add("Event not found or already deleted", Severity.Warning);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Context.Events.Remove(eventToDelete);
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
if (!_isDisposed)
|
||||
{
|
||||
Snackbar.Add($"Event {evt.Name} deleted", Severity.Info);
|
||||
Snackbar.Add($"Event {eventToDelete.Name} deleted", Severity.Info);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user