Fix: Import concurrency errors + Add sales data cleanup

- Fix optimistic concurrency errors in product import by using ExecuteSqlRaw instead of EF tracking
- Add DeleteAllOrdersAndSalesDataAsync() method to clear orders, payments, customers, and messages
- Add DeleteAllSalesData endpoint to ProductsController for admin access
- Proper deletion order to avoid foreign key violations
- Enhanced logging for troubleshooting data cleanup operations

Resolves: Import errors with 'database operation expected to affect 1 row(s)'
This commit is contained in:
2025-10-08 15:00:51 +01:00
parent 6c8106ff90
commit 86e30d7203
2 changed files with 120 additions and 22 deletions

View File

@@ -306,4 +306,21 @@ public class ProductsController : Controller
}
return Json(collection);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteAllSalesData()
{
try
{
var deletedCount = await _importService.DeleteAllOrdersAndSalesDataAsync();
TempData["SuccessMessage"] = $"✅ Successfully deleted {deletedCount} sales records (orders, payments, customers, messages)";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
TempData["ErrorMessage"] = $"Failed to delete sales data: {ex.Message}";
return RedirectToAction(nameof(Index));
}
}
}