开发者

Self-tracking entity Remove child entity

What is the right way to delete collection element from STE. Currently I use the f开发者_开发问答ollowing code:

order.Items[i].MarkAsDeleted();
order.Items.RemoveAt(i);

Looks like it works (and ApplyChanges removes entity in spite I have removed it from collection). But it looks a bit ugly. Am I doint it right? Or may be there are other ways?


This text is taken directly from MSDN:

The MarkAsDeleted method changes the state of the entity to Deleted. This method also clears the navigation properties on the entity that is being marked for deletion. The navigation property is set to null if it is pointing to a reference object. The Clear method is called if the navigation property represents a collection. When MarkAsDeleted is called on an object that is part of a collection, the object is removed from the collection. To mark each object in a collection as deleted, mark the objects in a copy of the collection. To get the copy of the collection, call the ToArray() or ToList() method on the collection, as in the following example:

List<Course> courses = department.Courses.ToList();
foreach (var c in courses)
{
    // Mark each course in the department as Deleted.
    c.MarkAsDeleted();
} 

So you are not doing it wrong because once you mark item as deleted it should be already removed from the Items collection so removing item at index will most probably remove break relation with another one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜