Search code examples
c#.netentity-frameworkentity-framework-4

Deleting Entity Object with 1 to Many Association to View-Based Entity


I have a Model-First entity model which contains a Customer table linked to a view that fetches customer details from a separate database. The relationship is One to Many between the Customer table and the View and I have a navigation property on both the Customer entity and the View entity.

When I try to perform a delete using context.Customers.DeleteObject(cust) and call context.SaveChanges() I get an error:

Unable to update the EntitySet 'ViewEntity' because it has a DefiningQuery and no [DeleteFunction] element exists element to support the current operation.


I have tried setting On Delete Cascade and None and both generate the same error.

EDIT: There's not much code to show, but here you go:

Customer selectedCust = (Customer)dgvCustomers.SelectedRows[0].DataBoundItem;
if (selectedCust != null)
{
    if (MessageBox.Show(String.Format("Are you sure you want to delete Customer {0}?", selectedCust.CustomerID.ToString()), 
                            "Customer Delete Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        // TODO - Fix this
        this.ReportSchedDBContext.Customers.DeleteObject(selectedCust);
        this.ReportSchedDBContext.SaveChanges();                      
    }
}

Solution

  • I was able to work around this issue by creating a dummy Stored Procedure that does nothing ("SELECT 'Done'") and using the SP Function Mapping in my two Views to set the Delete function to this Stored Procedure. Quite a hack but it worked.

    I'm not sure why a Delete Function is required for a View or if I'm doing something else wrong which caused the issue, but the above worked for me.