public async Task DeleteAsync(Guid id) { Ensure.That(id, nameof(id)).IsNotEmpty(); var domainPrimaryObject = await _primaryObjectRepoistory.GetByIdAsync(id); Ensure.That(domainPrimaryObject, nameof(domainPrimaryObject)) .WithException(_ => GetDemoEntityNotFoundException(id)) .IsNotNull(); foreach (var domainSecondaryObject in domainPrimaryObject.SecondaryObjects.ToArray()) { domainPrimaryObject.SecondaryObjects.Remove(domainSecondaryObject); _secondaryObjectRepository.Remove(domainSecondaryObject); } _primaryObjectRepoistory.Remove(domainPrimaryObject); await _unitOfWork.SaveChangesAsync(); }