public async Task DeleteAsync(int id) { var companyEntity = await _dbContext.Companies .Include(c => c.Invoices) .SingleOrDefaultAsync(c => c.Id == id); if (companyEntity == null) { throw new DBNotFoundException(); } var invoices = await _dbContext.Invoices .Where(i => i.Company.Id == id) .ToListAsync(); _dbContext.RemoveRange(invoices); _dbContext.Remove(companyEntity); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!_dbContext.Companies.Any(c => c.Id == id)) { throw new DBNotFoundException(); } }
public async Task DeleteAsync(int id) { var invoiceEntity = await _dbContext.Invoices .SingleOrDefaultAsync(c => c.Id == id); if (invoiceEntity == null) { throw new DBNotFoundException(); } _dbContext.Remove(invoiceEntity); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!_dbContext.Invoices.Any(i => i.Id == id)) { throw new DBNotFoundException(); } }