0

I want to execute a Db UPDATE whenever a particular object is Removed from the context in Entity Framework.

But as it is deleted in the end, EF does not execute an UPDATE in the Db.

Can I enforce EF to really execute a Db UPDATE and then DELETE the entity afterwards?

Thanks

PS : The reason I want to enforce an UPDATE before DELETE is because there is a trigger on the Db which helps me obtain some info for later use.

pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • 1
    How to alter the default Delete function to implement my own: http://stackoverflow.com/q/18985295/150342 – Colin Oct 10 '13 at 13:40
  • Colin, I can override the behaviour and maybe get the specific entries marked as delete; and I can mark them as Modified, call SaveChanges(). And re-mark them as DELETE. And SaveChanges() again -but I am not sure if it can have negative side effects to call Savechanges two times.. – pencilCake Oct 10 '13 at 13:50
  • Why not create a `DELETE` trigger instead, as that's what you're really doing? – Matthew Oct 10 '13 at 13:51
  • Bcoz, the data I need won't be achievable until I write it to Db -via Update. – pencilCake Oct 10 '13 at 13:58
  • I don't see there being a problem with calling SaveChanges twice - it's a standard way of coping with database exceptions for example http://msdn.microsoft.com/en-US/data/jj592904. The other way to do it is in a stored procedure: http://stackoverflow.com/a/19248216/150342 – Colin Oct 10 '13 at 14:03

1 Answers1

1

Triggers and EF are not a good combination as the triggers are largely opaque in your code and making someone do an update before a delete to ensure a particular trigger fires would be confusing.

Less hacky solutions:

If you have to use a trigger (i.e. the DB is used by other apps that rely on the trigger) modify your trigger or create a new trigger that fires on delete and saves the required information.

Encapsulate your trigger code in a method and ensure that method is used for all updates and deletes by your app.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79