Override delete function in entity structure

How can I make my own delete method to prevent actual data deletion? I want to set the datetime field when it will be deleted instead of the usual delete.

I read about overriding the subordination function, but I can't get it to work

+3
source share
3 answers

Process SavingChanges, view deleted items in context, change their state to changed, and change the corresponding field.

+4
source

IRequiredColumns CreatedOn, ModifiedOn DeletedOn, . :

Partial Public Class Context
Public Overrides Function SaveChanges(ByVal options As System.Data.Objects.SaveOptions) As Integer
    For Each entry As ObjectStateEntry In ObjectStateManager.GetObjectStateEntries(EntityState.Added Or EntityState.Modified Or EntityState.Deleted)
        If TypeOf (entry.Entity) Is IRequiredColumns Then
            Dim entity As IRequiredColumns = CType(entry.Entity, IRequiredColumns)

            Select Case entry.State
                Case EntityState.Added
                    entity.CreatedOn = Now
                Case EntityState.Modified
                    entity.ModifiedOn = Now
                Case EntityState.Deleted
                    entry.ChangeState(EntityState.Modified)
                    entity.DeletedOn = Now                        
            End Select
        End If
    Next

    Return MyBase.SaveChanges(options)
End Function
End Class

!

+1

If you don’t know how to override the delete function, you can create an ON DELETE trigger for each table in the database that does not delete, but sets the datetime field.

0
source

Source: https://habr.com/ru/post/1743507/


All Articles