【发布时间】:2011-07-20 13:48:45
【问题描述】:
我有一个按层次结构排列的类,例如:
class TestContainer
{
... bla bla bla...
public Virtual Item Items {get;set;}
}
Class Item
{
[Key]
public int ID {get;set;}
public string PropA {get;set;}
public string PropB {get;set;}
[InverseProperty("Parent")]
public virtual ICollection<Item> Children { get; set; }
[InverseProperty("Contents")]
public virtual Item Parent { get; set; }
}
所以,我有一个更新方法,可以让用户上传一组更新的孩子。
一开始我没有意识到,但删除我的容器 TestContainer 并不会删除项目。
好的,没问题吧?
我插入了以下函数(可能很丑但是我这里还处于原型阶段)
private void DeleteItems(TestContainer existingContainer)
{
//setup a flat list for deletion
List<Item> trashCan = new List<Item>();
//delete the old CI's from the database
BuildDeletionList(existingContainer.Items, ref trashCan);
foreach (Item item in trashCan)
{
context.Items.Remove(item);
}
}
private void BuildDeletionList(ICollection<Item> items, ref List<Item> trashCan)
{
if (items != null)
{
foreach (Item item in items)
{
BuildDeletionList(item, ref trashCan);
}
}
}
private void BuildDeletionList(Item item, ref List<Item> trashCan)
{
if (Item.Children != null)
{
foreach (Item child in Item.Children)
{
BuildDeletionList(item, ref trashCan);
}
}
item.Children.clear();
trashCan.Add(item);
}
这里的问题是,这要么使我的测试服务器崩溃,要么当我删除递归位(并且只删除父级 - 测试此时出了什么问题)时,我收到以下错误
“保存不为其关系公开外键属性的实体时发生错误。EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。”
我怎样才能最好地级联删除一个项目及其所有子项,以便我在数据库中没有孤儿? (就像我现在一样)
【问题讨论】:
标签: c# .net asp.net-mvc ef-code-first