我已经将我的数据网格绑定到列表。我正在关注Repository模式并使用EF v4.1。我需要删除row_deleting事件上的实体。这是代码:grdview_RowDeleting DataItem为空
protected void grdBooks_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int bookId = (int)e.Keys[0];
//grdBooks.Rows[e.RowIndex] //this item's dataItem is always null.
}
由于我正在使用实体,我需要实际的实体将它传递给我的GenericRepository,它将删除该实体。我确实得到了bookId,但我不想做一些愚蠢的事情,比如使用这个bookId从数据库中获取实体,然后把它传递给我的删除方法。为什么DataItem总是空,我能做些什么来取回我的实体?
感谢提前:)
我得到了异常如果我按照你的建议去做。 EntityKey为空,因此不能附加对象。 – TCM 2011-05-21 14:37:56
请参阅http://stackoverflow.com/questions/1007737/is-there-a-way-to-delete-an-entity-by-its-entitykey-witout-loading-it – 2011-05-21 15:24:30