I have a Dynamic Data page showing a ListView of items, each with a LinkButton to delete [CommandName=Delete]. I'm capturing the ItemDeleting event and cancelling it [e.Cancel = true], then doing a custom delete. First I create a new instance of my datacontext, then I use the deleted row's PK to get a new instance of the Entity to delete. Finally I pass the new entity to the DeleteOnSubmit(entity) function [myContext.myTable.DeleteOnSubmit(myEntity)], and SubmitChanges(). When I run the code, there are no errors, but the record does not get deleted. It's as if the code never ran. Here is my actual code:
protected void AdHocReportsListView_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
if (e.ItemIndex > -1)
{
EntitySet rpts = ((DataBoundControl)sender).DataSource as EntitySet;
if (rpts != null)
{
AdHocReport deletedRpt = rpts[e.ItemIndex];
using (MyDataContext context = new MyDataContext())
{
AdHocReport rpt = context.AdHocReports.Single(item => item.AdHocReportId == deletedRpt.AdHocReportId);
context.AdHocReports.DeleteOnSubmit(rpt);
context.SubmitChanges();
}
}
}
e.Cancel = true;
}