none
Update data before delete RRS feed

  • Question

  • Hi,

    May i know how to update data to db before delete it from db by LINQ.
    The reason is I want to record down who has deleted the record, i want to store the user and date to database, so that my sql trigger in database can capture it and write a custom log.

    Right now, my code is as below. But now it direct executes the delete, ignore my update

    //Update record
    record.ModifiedBy = CurrentUser;
    record.ModifiedDate = DateTime.Now

    //Delete record
    dbManager.UserContext.DeleteOnSubmit(record);
    dbManager.SubmitChanges();

    Tuesday, November 17, 2015 7:01 AM

Answers

  • >>but that guys is using entity framework, wonder the similar solution can be applied to LINQ as well, but have no idea how to to do it.

    After checking your link, the guy is doing a soft-delete operation instead of deleting the record from the database, you could also use this approach in LIQN to SQL as below:

    //a soft-delete
    
     record.ModifiedBy = CurrentUser;
    
     record.ModifiedDate = DateTime.Now;
    
     record.State = “Deteletd”;
    
     dbManager.SubmitChanges();
    

    While this approach needs you to add an additional column in you database.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 18, 2015 2:08 AM
    Moderator
  • If you want your database trigger to activate on a change you will have to notify the database of the update.  That is done with a SubmitChanges.  No update , no trigger.

    Lloyd Sheen

    Wednesday, November 18, 2015 2:57 PM

All replies

  • Linq is being smarter than I would have thought. 

    Your code has two lines which mark the "record" as changed.  Then it marked as deleted.

    You have only one SubmitChanges so it looks as if Linq knows the record is deleted so why do an update first.  Add another SubmitChanges after the "record" is updated and Linq should pass an update prior to the delete.


    Lloyd Sheen

    Tuesday, November 17, 2015 2:17 PM
  • Is it the only way? Somehow my code is a bit of complex, has some data changes by looping through the change set as well, so it will be harder to split the update and delete out.
    Wonder is there any easier solution?

    saw this post, https://social.msdn.microsoft.com/Forums/en-US/98bd2995-bc6f-4786-b721-8c37c226942b/updating-an-object-before-deletion, the solution is changing the object state.but that guys is using entity framework, wonder the similar solution can be applied to LINQ as well, but have no idea how to to do it.

    Wednesday, November 18, 2015 1:24 AM
  • >>but that guys is using entity framework, wonder the similar solution can be applied to LINQ as well, but have no idea how to to do it.

    After checking your link, the guy is doing a soft-delete operation instead of deleting the record from the database, you could also use this approach in LIQN to SQL as below:

    //a soft-delete
    
     record.ModifiedBy = CurrentUser;
    
     record.ModifiedDate = DateTime.Now;
    
     record.State = “Deteletd”;
    
     dbManager.SubmitChanges();
    

    While this approach needs you to add an additional column in you database.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 18, 2015 2:08 AM
    Moderator
  • If you want your database trigger to activate on a change you will have to notify the database of the update.  That is done with a SubmitChanges.  No update , no trigger.

    Lloyd Sheen

    Wednesday, November 18, 2015 2:57 PM