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

✅ EF Core Query Filter Exception

Hi,
I'm working with EF Core with PostgreSQL, using Code First approach,
I have this Entity Type Configuration:
public class CategoryEntityConfig : IEntityTypeConfiguration<CategoryEntity>
{
    public void Configure(EntityTypeBuilder<CategoryEntity> builder)
    {
        builder.HasKey(c => c.Id);
        
        builder.Property(c => c.Name).IsRequired().HasMaxLength(50);
            
        builder.Property(c => c.Description).IsRequired(false);


        builder.Property(c => c.CreatedAt).IsRequired(true);
        builder.Property(c => c.CreatedBy).IsRequired(true);
        
        builder.Property(c => c.LastUpdatedAt).IsRequired(false);
        builder.Property(c => c.LastUpdatedBy).IsRequired(false);
        builder.Property(c => c.DeletedAt).IsRequired(false);
        builder.Property(c => c.DeletedBy).IsRequired(false);
        


        builder.HasOne(c => c.User)
            .WithMany(u => u.Categories)
            .HasForeignKey(c => c.UserId)
            .OnDelete(DeleteBehavior.Cascade);
            
        
        builder.HasQueryFilter( e => OnlyNonDeletedCategoriesQueryFilter(e));
    }
    
    
    /// <summary>
    /// Determines if a category entity is not deleted.
    /// </summary>
    /// <param name="categoryEntity">The category entity to check.</param>
    /// <returns><c>true</c> if the category entity is not deleted; otherwise, <c>false</c>.</returns>
    private static bool OnlyNonDeletedCategoriesQueryFilter(CategoryEntity categoryEntity)
    {
        return categoryEntity.IsDeleted == false;
    }
}


After running the app I get an exception that tells me that the EF Core couldn't translate that query filter to SQL, as shown in the attached video.

Please any help to fix this issue guys ???
and massive thanks in advance <3
Was this page helpful?