void replaceListInDB <T>(ICollection <T> listToInsert, ICollection <T> listToReplace, IEqualityComparer <T> comparer) { var substraction = listToReplace.Except(listToInsert, comparer).ToList(); foreach (var i in substraction) { _context.Remove(i); } foreach (var i in listToInsert) { /*try * {*/ _context.Add(i); /*} * catch (InvalidOperationException e) * { * _context.Update(i); * }*/ } }