Skip to content

How to apply unique constraint in case of global query filter #25401

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

Closed
nicolapiccinelli opened this issue Aug 3, 2021 · 1 comment
Closed

Comments

@nicolapiccinelli
Copy link

Hello,
I cannot find anyway a solution for the following behaviour (maybe is by design, but I cannot figure out if it is): i've setup a query filter to provide global soft delete to my context, but it seems to be ignored by the unique check. Can anyone give me ad advice if there is a solution to achieve this? Right now I'm getting an exception in case of deleted records.

foreach (var type in modelBuilder.Model.GetEntityTypes())
{
    if (type.ClrType.BaseType == typeof(Models.Entities.EFExtensions.BaseEntity))
    {
        modelBuilder.SetSoftDeleteFilter(type.ClrType);
    }
}

modelBuilder.Entity<MyClass>().HasIndex(p => p.Name).IsUnique();

EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqLite
Target framework: .NET 5.0
Operating system: Linux/MacOs/Windows
IDE: Visual Studio Code

@ajcvickers
Copy link
Contributor

@nicolapiccinelli Setting up this kind of filter on the index automatically is something that we would likely do as part of supporting soft-delete across the stack. It is not something that will happen automatically when setting a query filter. However, you can manually add a filter to the index in your model building code.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants