Often I meet in other projects the following.

An entity has the boolean property IsDeleting. And when deleting from the database, the object is not deleted, instead, the property IsDeleting = true. Well, respectively, all the objects for which IsDeleting = true are not displayed in the DataGrid.

Why do they do that? Is it possible that then any entity could be easily restored.

  • one
    Microsoft recommends doing this a little differently : you tell DbContext 'that you want to delete an object, and it actually deletes this and other deleted objects on SaveChanges() . Why do other projects do this? Cost to ask them (well, or see how they use this property). In any case, immediately removing the object from the database does not make sense: communication with the database is an expensive operation, so it is better to accumulate many changes and synchronize with the database in one sitting. - VladD

2 answers 2

Rather, not IsDeleting, but IsDeleted. The object is simply marked as deleted. It remains physically, but this feature in the entire logic of the application causes this object to not see. This is done in order to save the data. If the object is deleted, then everything is practically irreversible. Say, if by mistake your application deletes all users - will you be upset? And if there was just this sign, then all the records would remain. By the way, in addition to the Boolean version, there is still time and the same as the ID - a link to the log.

    If a logical deletion is planned to be made for the purpose of saving data for reporting, then there is an alternative to simpler Snapshots . This is not exactly the same thing, but sometimes it will be a better solution than logical deletion, since you can not store dates for deleting, updating, inserting in all tables, but keeping the date of relevance, for example, in the database name.