Turning off global filter warnings in EFCore

Viewed 1364

I have a filter on one of my entities

 entity.HasQueryFilter(x => !x.IsDeleted);

and I keep getting the following warnings in logs which I like to turn off

Entity '"Event"' has a global query filter defined and is the required end of a relationship with the entity '"EventCategory"'. This may lead to unexpected results when the required entity is filtered out. Either configure the navigation as optional, or define matching query filters for both entities in the navigation. See https://go.microsoft.com/fwlink/?linkid=2131316 for more information.

2 Answers

If you are certain that your entity configuration is correct and want to turn off that particular warning you can use the DbContextOptionsBuilder.ConfigureWarnings method.

Assuming you register the DbContext using IServiceCollection:

using Microsoft.EntityFrameworkCore.Diagnostics;

services.AddDbContext<MyDbContext>(options =>
{
    // Other configuration here...

    options.ConfigureWarnings(builder =>
    {
        builder.Ignore(CoreEventId.PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning);
    });
});

Be aware that this will silence all warnings of this type.

 modelBuilder.Entity<EventCategory>()
.HasQueryFilter(t => !t.Event.IsDeleted);

you should filter it from all entites that related to Event

Related