dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.77k stars 3.18k forks source link

Soft Delete #4025

Open ghost opened 8 years ago

ghost commented 8 years ago

Hello.

Any suggestions on how to implement a soft delete at Entity Framework 7 ?

Any help is much appreciated.

Regards.

rowanmiller commented 8 years ago

We're putting this one on the backlog as a feature we would like to have at some point.

In the meantime, you probably could do this by overriding some of the low level components... but that is going to be fairly complex to do. If you decide to try... then you will probably make things less painful if you have your soft delete flag mapped as a shadow state property. For SaveChanges, you could look at a custom implementation of ICommandBatchPreparer.BatchCommands to generate updates rather than deletes for state entries that are marked as deleted.

cc @anpete in case he can give you a pointer of where to look for query.

anpete commented 7 years ago

Enabled in b1379b10f

seriouz commented 7 years ago

I think we definitely need an opt-out operator like: .NotFiltered()

smitpatel commented 7 years ago

@seriouz - The opt-out operator is named IgnoreQueryFilters Ref: https://github.com/aspnet/EntityFramework/blob/dev/src/EFCore/EntityFrameworkQueryableExtensions.cs#L2462

smitpatel commented 7 years ago

@anpete - Is this issue fully resolved? If yes then we should close the issue and set the milestone.

cc: @divega

anpete commented 7 years ago

Unless we want to track a specific high-level Soft Delete feature.

ajcvickers commented 7 years ago

I think we do want to track that feature. Specifically, I should be able to delete objects normally in my graph or with .Remove, and then have it automatically soft delete those entities.

divega commented 7 years ago

Agreed. Query filters only solves a part of what we need.

ajcvickers commented 6 years ago

Note to implementer: remember to make cascade delete (of tracked entities) work.

Geestarraw commented 1 year ago

This issue has been living for some time. Are there any plans to implement it?

roji commented 1 year ago

This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 8.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.

qsdfplkj commented 1 year ago

To me soft deletes don't make sense. On a database level: Any unique constraint will break. What about a foreign key that has been soft deleted? Do we need cascading soft deletes? You could build an entire shadow database with soft deleted entities. To me soft deletes should be related to business logic, e.g. add an start and end date to a subscription. Soft delete will only work on database that do not have any constraints but then the problem is just moved to the point of where we need to read the data.

Maybe temporal tables can be used for soft deletes.

seriouz commented 1 year ago

Yes soft deletes should respect cascade delete. Sure you can implement it easy by youself with thousands of possibilities. But i imagine an EF Core Soft Delete like a ready-to-go solution - where i (as a dev) have not to know how its working behind the scenes and i don't have to touch the queries (apart from when i must read/recover deleted data).