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.68k stars 3.17k forks source link

Expression<Func<TEntity, bool>> as parameter in RelationalIndexBuilderExtensions.HasFilter #19653

Open doboczyakos opened 4 years ago

doboczyakos commented 4 years ago

Let's have an entity:

public class MyEntity : IEntityTypeConfiguration<MyEntity>
{
    public Guid Id { get; set; }
    public string Description { get; set; }
    public MyEnum Enum { get; set; }

    public void Configure(EntityTypeBuilder<MyEntity> builder)
    {
        builder.Property(e => e.Description).HasMaxLength(200);
    }
}

public enum MyEnum
{
    Value1,
    Value2,
    Value3
}

Let's define an index with filter and include: builder.HasIndex(e => e.Description).HasFilter($"[Enum] = {(int)MyEnum.Value1}").IncludeProperties(e => e.Id);

It would be nice to define the index filter the following way: builder.HasIndex(e => e.Description).HasFilter(e => e.Enum == MyEnum.Value1).IncludeProperties(e => e.Id);

ajcvickers commented 4 years ago

Note for team: fluent API for HasFilter, previously discussed here: https://github.com/dotnet/efcore/issues/7087#issuecomment-262472914

cts-tradeit commented 8 months ago

Hi @ajcvickers, we are facing similar issue with entity configuration that is supposed to be provider agnostic. Raw filter can be cross compatible only in certain circumstances (when DB does not require special characters around names such as [] for SQL Server or "" for PGSQL, however, these coditions vary across the DB impls).

We have created a workaround that uses dependencies such as ISqlGenerationHelper to properly translate the filter to raw sql.

I have discovered several PRs from the last decade that discuss this possibility (and contain merged PRs) yet this feature has never been realiazed.

Is there any blocker that makes this unrealisable?

roji commented 8 months ago

@cts-tradeit there isn't necessary any blocker here, but the issue has received only 8 votes since it was opened, showing very little user interest. That's why it's not being prioritized - especially given that there's a reasonable workaround (i.e. specify the filter as SQL instead).

cts-tradeit commented 8 months ago

@roji I would not call that a reasonable workaround especially when using IEntityTypeConfiguration interface that does not have out of the box access to DbContext nor DatabaseFacade. U may inject them via ctor but that prohibits you from using ApplyConfigurationsFromAssembly and forcing u to either instance all configurations manually or write custom boilerplate code.

It would be way smoother to translate the filter in MigrationsSqlGenerator instead of doing this in every implementation of IEntityTypeConfiguration.

I'd attribute the lack of interest to overall low interest in filtered indexes, not that the current implementation is good enough.

somegenericdev commented 3 months ago

I honestly couldnt believe my eyes when I saw there was no overload that took an expression as the parameter. I think this issue should be given more attention, current implementation seems really half assed.