© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
37 replies
M B V R K

EF Core Global Query Filter Exception

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


In my
AppDbContext
AppDbContext
=>
OnModelCreating
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 );
            }
        }
        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
IArchivable
looks like:

    public interface IArchivable
    {
        bool?     IsArchived { get; set; }
        string    ArchivedBy { get; set; }
        DateTime? ArchivedOn { get; set; }
    }
    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
exception
:
InvalidOperationException: The filter expression 'x => ((Convert(x.IsArchived, Nullable
1) != null) OrElse (Convert(x.IsArchived, Nullable
1) != null) OrElse (Convert(x.IsArchived, Nullable
1) == 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
ContinuousFormation
is an
Entity type/class
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; }
    }
 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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ EF Core Query Filter Exception
C#CC# / help
3y ago
EF Core Global Query Filter with Nullable Exception
C#CC# / help
4y ago
✅ Ef-core query efficiency?
C#CC# / help
15mo ago
Why only one EF Core Global Query Filter work but others are not
C#CC# / help
4y ago