我有一系列儿童用品,我不想删除,然后添加新的.
我不关心性能,因为它是一种非常频繁的操作.
我不关心性能,因为它是一种非常频繁的操作.
我该怎么办?我尝试过Order.items.clear()和Order.Items.Remove(x),但两者都给我例外
简化代码:
Dim db As New MainDataDataContext dim o as Order = (From Order In db.Orders Where Order.OrderID = OrderID).FirstOrDefault ''//this will return "An attempt was made to remove a ''//relationship between a Order and a OrderItem. ''//However,one of the relationship's foreign keys ''//(Order.OrderID) cannot be set to null." exception o.Items.Clear() ''//and this will return "EntitySet was modified during enumeration." exception For Each oItem As OrderItem In o.Items o.items.Remove(oItem) Next For Each item As ListViewItem In listViewOrderItems.Items If item.ItemType = ListViewItemType.DataItem Then Dim oItem As New OrderItem oItem.OrderID = OrderID oItem.Product = CType(item.FindControl("txtProduct"),TextBox).Text oItem.Quantity = CType(item.FindControl("txtQuantity"),TextBox).Text Order.items.Add(oItem) End If Next db.SubmitChanges()
解决方法
只要您删除所有项目,您也可以使用
db.OrderItems.DeleteAllOnSubmit(o.Items);