C#C
C#4y ago
M B V R K

EF Core Global Query Filter Exception

Hi friends, I'm try to create a Global query filter for my EF Core, to be honest this is the first time for me with Global Query Filters

In my AppDbContext => OnModelCreating I tried this:
        foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
        {
            if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
            {
                Expression<Func<IArchivable, bool>> exression = x=> (bool?) x.IsArchived != null || (bool?) x.IsArchived == false;   
              
                modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( exression );
            }
        }

How my IArchivable looks like:
    public interface IArchivable
    {
        bool?     IsArchived { get; set; }
        string    ArchivedBy { get; set; }
        DateTime? ArchivedOn { get; set; }
    }

The Issue:
When I start the app, automatically II get this exception:
InvalidOperationException: The filter expression 'x => ((Convert(x.IsArchived, Nullable1) != null) OrElse (Convert(x.IsArchived, Nullable1) == Convert(False, Nullable`1)))' specified for entity type 'ContinuousFormation' is invalid. The expression must accept a single parameter of type 'MBSM.Core.Entities.ContinuousFormation' and return bool.

Which ContinuousFormation is an Entity type/class and it looks like :
 public class ContinuousFormation : IAuditable, IArchivable
    {
       //Other properties removed for clarity
        public bool ?     IsArchived   { get; set; }
        public string     ArchivedBy   { get; set; }
        public DateTime ? ArchivedOn   { get; set; }
    }


My Questions:
Please how do I can fix this issue ? and massive thanks in advance <3
Was this page helpful?