© 2026 Hedgehog Software, LLC

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

EF Core Global Query Filter with Nullable 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 ) )
            {
                var parameter = Expression.Parameter( entityType.ClrType , "p" );
                var archivedCheck = Expression.Lambda(Expression.Equal(Expression.Property(parameter, "IsArchived"), Expression.Constant(false)), parameter);
                modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( archivedCheck );
            }
        }
  foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
        {
            if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
            {
                var parameter = Expression.Parameter( entityType.ClrType , "p" );
                var archivedCheck = Expression.Lambda(Expression.Equal(Expression.Property(parameter, "IsArchived"), Expression.Constant(false)), parameter);
                modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( archivedCheck );
            }
        }

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 binary operator Equal is not defined for the types 'System.Nullable`1[System.Boolean]' and 'System.Boolean'.

I think the issue related with the
Nullable
Nullable
IsArchived
IsArchived
property, may I need to add an
OR
OR
to my
Expression
Expression
,

My Questions:
What is the cause of that issue?
If the
Nullable
Nullable
of the
IsArchived
IsArchived
how to fix it ?
If I need to ad an
OR
OR
to my
expression
expression
to check the
Nullability
Nullability
of
ISArchived
ISArchived
how to do it ?

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 Global Query Filter Exception
C#CC# / help
4y ago
✅ EF Core Query Filter Exception
C#CC# / help
3y ago
EF Core Nullable Foreign Key Relationship
C#CC# / help
7mo ago
✅ Ef-core query efficiency?
C#CC# / help
15mo ago