Следующий код работает. Тем не менее, это работает, потому что я в конечном итоге создаю глубокий клон поставщиков. Если я не выполняю глубокое клонирование, мы получаем ошибку, указывающую на то, что объекты поставщика изменились и попытка изменить таблицу поставщиков не удалась Это происходит только при выполнении следующей строки: foreach (Supplier suppliers in exceptions)
. Как ни странно, это происходит независимо от того, выполняется ли метод Delete()
. Почему это происходит? Я отправил рабочий код ниже для вашей проверки. Как я уже сказал, если вы попытаетесь зациклить без глубокого клонирования, то это не сработает ... Есть идеи?
public void DeleteSuppliers(IList<Supplier> suppliers, Int32 parentID)
{
// If a supplier has been deleted on the form we need to delete from the database.
// Get the suppliers from the database
List<Supplier> dbSuppliers = Supplier.FindAllByParentID(parentID);
// So return any suppliers that are in the database that are not now on this form
IEnumerable<Supplier> results = dbSuppliers.Where(f => !Suppliers.Any(d => d.Id == f.Id));
IList<Supplier> exceptions = null;
// code guard
if (results != null)
{
// cast as a list
IList<Supplier> tempList = (IList<Supplier>)results.ToList();
// deep clone otherwise there would be an error
exceptions = (IList<Supplier>)ObjectHelper.DeepClone(tempList);
// explicit clean up
tempList = null;
}
// Delete the exceptions from the database
if (exceptions != null)
{
// walk the suppliers that were deleted from the form
foreach (Supplier suppliers in exceptions)
{
// delete the supplier from the database
suppliers.Delete();
}
}
}