Soo... I 'm now struggling with a difficult Task.
I have this recursive method:
private async Task DeleteUnitsCascade(int unitID) {
var units = await _context.Units
.Where(u => u.ParentID == unitID &&
u.FromDate <= DateTime.Now &&
(u.ToDate == null || u.ToDate >= DateTime.Now))
.GroupBy(unit => unit.UnitID)
.Select(grouping => grouping.OrderByDescending(unit => unit.Version).FirstOrDefault())
.ToListAsync();
if (!units.Any()) return;
foreach (var unit in units) {
var entityEntry = _context.Units.Attach(unit);
entityEntry.Entity.ToDate = DateTime.Now;
entityEntry.Entity.Position = 0;
await DeleteUnitsCascade(unit.UnitID);
}
}
And as you see I've tried to use async
, but it have to wait for Task to end so it's like normal synchromous method. When I change:
await DeleteUnitsCascade(unit.UnitID);
To
DeleteUnitsCascade(unit.UnitID);
It's causing some strange results with MySQLConnector and DBContext. I've read a few similar question (Question), but it didn't help me understand. Also I've found this interesting article.
Can I somehow make all this recursive call run simultaneously?