private void Remove() { if (_comparer == null) { throw new ArgumentNullException(); } var toRemove = (_updates == null) ? _existing : _existing.Where(e => !_updates.Any(u => _comparer(e, u))).ToList(); var enumerable = toRemove as T[] ?? toRemove.ToArray(); foreach (var t in enumerable) { if (_context.GetEntityState(t) == EntityState.Detached) { _context.Set <T>().Attach(t); } } _context.Set <T>().RemoveRange(enumerable); }
/// <summary> /// Deletes Docs if relevant to type. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="docId"></param> protected void DeleteDocument <T>(int id, int docId) where T : Entity, IHasDocuments <Document> { var document = Context.Documents.Find(docId); ThrowIfNull(document); // ReSharper disable once PossibleNullReferenceException string fp = DocHelper.PrependDocsPath(document.FilePath); if (Context.GetEntityState(document) == EntityState.Detached) { Context.Documents.Attach(document); } var entity = Context.Set <T>() .Include(t => t.Documents) .SingleOrDefault(t => t.Id == id); entity?.Documents.Remove(document); Context.Documents.Remove(document); Context.SaveChanges(); File.Delete(fp); }