开发者

EntityDataSource Override with a custom Delete

开发者 https://www.devze.com 2023-01-27 02:48 出处:网络
I have an ASP .net C# project using a EntityDataSource with a DevExpress asp开发者_如何学运维xGridView and it is working great allowing me to select, update, insert and delete. However I would like to

I have an ASP .net C# project using a EntityDataSource with a DevExpress asp开发者_如何学运维xGridView 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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消