English 中文(简体)
实体框架; 如何处理每个圆环中的例外并保持迭接
原标题:Entity Framework; How to handle an exception in foreach loop and keep iterating

当我通过一个带有以下代码的预言切换时,它成功抓住了第一个出现的例外,并在我的错误列表中添加了代号。在循环的所有后续迭代中,它将继续捕捉上一个例外。

我怎样才能适当抓住例外,撤销或清除失败的删除请求,以便随后执行删除。

public ActionResult Delete(int[] ListData)
{
    List<int> removed = new List<int>();
    List<int> error = new List<int>();
    Item deleteMe;
    foreach (var id in ListData)
    {
        deleteMe = this.getValidObject(id);
        if (deleteMe == null)
        {
            error.Add(id);
            continue;
        }

        try
        {
            this.DB.Items.DeleteObject(deleteMe);
            this.DB.SaveChanges();
            removed.Add(id);
        }
        catch (DataException ex)
        {
            // revert change to this.DB.Items?
            error.Add(id);
        }
    }
    if (error.Count > 0)
    {
        return Json(new { Success = false, Removed = removed, Error = error });
    }
    return Json(new { Success = true, Removed = removed });
}

我搜索了 SO 和 Google, 大多数人会先处理所有的删除对象, 然后保存更改, 这样它就是一个交易。 但我需要它单独处理每笔交易, 这样单项失败不会阻止其余的交易 。

我在使用实体框架4。

我得到的例外是这个具体例子,因为外国钥匙与正在删除的项目相关联。 在制作过程中,我将处理这种情形,但它应该能够继续,不管例外是什么。

最佳回答

我想,在 this.getValidObject(id) 中,使用相同的上下文 this.getValidObject(id) 来检索一个实体。 如果是这种情况,在例外区块呼叫中: this.DB.detach(detem) 。这将防止 SaveChanges() 在下次迭代时删除有问题的实体。

问题回答

您所展示的代码看起来不错。 您看到的错误是什么? 正如您已经指出的, 您也许需要在这个. DB. 项中解牌 。 虽然我不这么认为, 但项目。 您也可以尝试为每个循环创建一个新的数据附件, 这样旧的、 失败的数据附件状态就无关紧要了 。

If I understood correctly, you cannot remove the entity(Item) because it has a foreign key association(child) to it.
You will first have to update all child(related) entities using the Parent(Item) you want to delete, by removing the relationship, updating the entity to relate too an alternative parent(Item) or deleting the child entity(entities) and then finally removing the Parent(Item) entity.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签