Customizing what the Update Sql Code does RRS feed

  • Question

  • I was wondering if it was possible to edit the SQL that is actually being sent to the Database.  I am primarily interested on update.  I would like to add sql before and after the update code.  I do not want to use triggers, and do not want to write the code in C#. 



    Friday, March 15, 2013 3:03 PM

All replies

  • Hi,

    I would recommend to start by explaining what you are trying to do rather than how you plan to do it. Not sure what you meant by not writing C# code (even overriding SaveChanges to send SQL request to the db before and after the core SaveChanges ?)

    If you really don't want to write any C# at all nor to alter anything server side I'm not sure what magic thing to expect. Try to explain first your goal.

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, March 15, 2013 3:48 PM
  • I have a very large amount of disconnected traffic coming in.  If a column changes, I want to insert an record into another table.  Problem being is I am not allowed to use triggers, and these actions are really business driven.

    If I am to detect that a column changed in C# I must make a call to the database to get the original values which is another trip to the database.  Potentially this can be just one call to the database that can do both actions.

    I have started looking over the EF APIs to see if there was an area that I could edit, but haven't be able to find it yet.

    Saturday, March 16, 2013 5:18 AM
  • With EF you should be able to get the value you had when the row was loaded using http://msdn.microsoft.com/en-us/library/gg679503(v=vs.103).aspx

    See for example http://jmdority.wordpress.com/2011/07/20/using-entity-framework-4-1-dbcontext-change-tracking-for-audit-logging/ that seems to match what you are looking for (and possibly more)

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Saturday, March 16, 2013 9:46 AM
  • That function does not work in a disconnected usage, you have to use GetDatabaseValues which is an extra call to the db.
    Thursday, April 4, 2013 12:54 AM