Question

I have an ASP .net C# project using a EntityDataSource with a DevExpress aspxGridView and it is working great allowing me to select, update, insert and delete. However I would like to use a custom delete method instead which basically just does an update instead (just setting an active flag to false) rather than a real delete.

I have a feeling I need to use the entitydatasource's or the grids onrowdeleting event but this is my first project with Entity Framework 4.0 so still finding my feet. I have no idea if I need to create an overrides update method in the edmx code behind file.

Greatly appreciate any help.

Was it helpful?

Solution

You can register a handler on the SavingChanges event to perform wat you want. something like this:

public partial class AWEntities{ 

partial void OnContextCreated()
{
    this.SavingChanges += new EventHandler(context_SavingChanges);// Register the handler for the SavingChanges event.
}

private static void context_SavingChanges(object sender, EventArgs e)// SavingChanges event handler.
{
    // Get all in Deleted state
    foreach (ObjectStateEntry entry in
        ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
    {
        if (entry.Entity.GetType() == typeof(MyType)))
        {
            // do what you want.
        }
    }
}
}

http://msdn.microsoft.com/en-us/library/cc716714.aspx

OR You can map a Stored Procedure to perform your delete the way you want. http://learnentityframework.com/LearnEntityFramework/tutorials/using-stored-procedures-for-insert-update-amp-delete-in-an-entity-data-model/

I like better the second option...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top