user2265414
user2265414

Reputation: 111

entity framework: own ondelete action

When any record deletes from some table (let us call it STORAGE_INFO) additional data from disk must be deleted.Could anyone provide a sample (if it possible yet) how to specify own DELETE action? Something like this:

      DBSet<STORAGE_INFFO>.ONDELETE+= CUSTOM_FUNCTION

      CUSTOM_FUNCTION()
      {
          BASE.DELETE();
          DELETE_DISK_CONTENT();
      }

Upvotes: 2

Views: 139

Answers (1)

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64923

Probably you're looking for the ObjectContext.SavingChanges event

If you iterate the result of calling the context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted) method using the whole overload, you'll be able to detect which entities were deleted when the ObjectContextsaved some changes.

Check the sample code in the MSDN article (it's a good sample of how to achieve your goal!):

The original poster said:

this is pretty.But where can I find this event in DBContext class?

Upcast the DbContext to IObjectContextAdapter:

((IObjectContextAdapter)dbContext).ObjectContext.SavingChanges += ...

Upvotes: 2

Related Questions