How can i make my own delete method to prevent that the data really gets deleted? i want to set a datetime field when it gets deleted insted of a normal delete.
i read about overriding the submitchanges function, but i don't get it to work
thanks
How can i make my own delete method to prevent that the data really gets deleted? i want to set a datetime field when it gets deleted insted of a normal delete.
i read about overriding the submitchanges function, but i don't get it to work
thanks
If you won't find out how to override the delete function, you can create an ON DELETE trigger for every table in database, that does not delete, but sets datetime field.
Handle SavingChanges
, go through the deleted items in the context, change their state to modified, and modify the field in question.
I wrote an interface IRequiredColumns with the properties CreatedOn, ModifiedOn and DeletedOn which every entity implements. Then I created this partial class for the context:
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
This works great for me!