C
C#3y ago
M B V R K

EF Core Global Query Filter with Nullable 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 ) )
{
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 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:
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 IsArchived property, may I need to add an OR to my Expression, My Questions: What is the cause of that issue? If the Nullable of the IsArchived how to fix it ? If I need to ad an OR to my expression to check the Nullability of ISArchived how to do it ? Massive thanks in advance <3
1 Reply
M B V R K
M B V R KOP3y ago
Screenshot of the Exception:

Did you find this page helpful?