0

我的 RadGrid 是从会话状态中保存的实体框架 ICollection 填充的。主键是数据库中的自动递增字段。用户可以将多行添加到网格而不将它们提交到数据库。如果用户在保存之前删除了一行,你如何找到要删除的特定实体?到目前为止,在实际保存数据之前,不会将主键分配给新行。

DataContext context = (DataContext)Session["context"];

protected void rgEmployees_DeleteCommand(object source, GridCommandEventArgs e) {
    int MyID = Tools.GetInt((e.Item as GridDataItem).
        OwnerTableView.DataKeyValues[e.Item.ItemIndex]["My_ID"]);
    Employee ee= context.Employee.SingleOrDefault(t => t.ID == MyID); 
    // The problem with above line is that t.ID has not been established yet.
    context.Employee.Remove(ee);
}
4

1 回答 1

0

假设实体将按照您添加它们的顺序排列,我认为您在这里唯一的选择是通过索引来完成,即

protected void rgEmployees_DeleteCommand(object source, GridCommandEventArgs e) {
    Employee ee = context.Employee.AsEnumerable().ElementAt(e.Item.ItemIndex); 
    context.Employee.Remove(ee);
}
于 2013-11-11T17:03:53.197 回答