-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core index and soft delete entity. #1181
Comments
HasFilter requires sql statement may have database compatibility issues. @hikalkan |
Normally, developer should care if the entity is soft delete and apply index based on it, like However, this is also not the final solution. Think that you have deleted an entity named "Foo", then created another entity with name "Foo", then you can not delete it since there will be two deleted "Foo" in this case. I didn't know |
Thank you for your explanation. |
A possible alternative to the IsDeleted issue is for IsDeleted to not be saved to the database and only save DeletionTime. IsDeleted could be a property that checks if DeletionTime is null => return !this.DeletionTime.HasValue. Then you could do a unique index on { e.Name, e.DeletionTime }, cover both the Name and DeletionTime in the index, it would enforce that only one e.Name exists per value which is not deleted, and allows you to accumulate multiple soft deleted records as each would have a different time. But of course this would change the way the base class is implemented and how the data filter works. I'm considering two indexes instead as a workaround to avoid making those changes:
|
b.HasIndex(e => e.Name).IsUnique();
If the entity supports soft deletion, there may be a problem.
When you delete an entity and continue adding the same entity, the index will prevent the addition.
HasFilter
seems to work, but needs to continue investigation.b.HasIndex(e => e.Name).IsUnique().HasFilter("[IsDeleted] != 1");
The text was updated successfully, but these errors were encountered: