- Background
We are trying to archive old user data to keep our most common tables smaller.
Issue
Normal EF code for removing records works for our custom tables. The AspNetUsers table is a different story. It appears that the way to do it is using _userManager.Delete or _userManager.DeleteAsync. These work without trying to do multiple db calls in one transaction. When I wrap this in a transactionScope, it times out. Here is an example:
public bool DeleteByMultipleIds(List<string> idsToRemove) { try { using (var scope = new TransactionScope()) { foreach (var id in idsToRemove) { var user = _userManager.FindById(id); //copy user data to archive table _userManager.Delete(user);//causes timeout } scope.Complete(); } return true; } catch (TransactionAbortedException e) { Logger.Publish(e); return false; } catch (Exception e) { Logger.Publish(e); return false; } }
Note that while the code is running and I call straight to the DB like:
DELETE
FROM ASPNETUSERS
WHERE Id = 'X'
It will also time out. This SQL works before the the C# code is executed. Therefore, it appears that more than 1 db hit seems to lock the table. How can I find the user(db hit #1) and delete the user (db hit #2) in one transaction?