Skip to content

Disable QueryFilter for Root Tenant Globally #922

Open
@eluvitie

Description

Hi there

In my web API I have a root tenant, which should be able to access all the data that the tenants have.

Here is some Information about the Project:

  • .NET 8 Web API
  • Postgresql
  • EF Core
  • Ardialis Specifications
  • Clean Architecture
  • Tenants Share the same Database
  • Finbuckle Multitenancy

Currently I set on each query the IgnoreGlobalQuery() and then set manaully the Filter for the SoftDelete Flag. This approach works if i only load one Entity, the problem is when i try to load child elements, because i'd have to set the SoftDelete Filter for each related Entity or else it displays all the deleted child elements.

So here is my question, is there a way to disable the global filter queries for the tenantId just for one Tenant (in my case the root Tenant)?

In my Optinion a nice way would be to set the filter (or disable) it when the DbContext for the Tenant gets initialized.

Here is my BaseDbContext:

public abstract class BaseDbContext : MultiTenantIdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>, IdentityUserRole<string>, IdentityUserLogin<string>, ApplicationRoleClaim, IdentityUserToken<string>>
{
    protected readonly ICurrentUser _currentUser;
    private readonly ISerializerService _serializer;
    private readonly DatabaseSettings _dbSettings;
    private readonly IEventPublisher _events;

    protected BaseDbContext(ITenantInfo currentTenant, DbContextOptions options, ICurrentUser currentUser, ISerializerService serializer, IOptions<DatabaseSettings> dbSettings, IEventPublisher events)
        : base(currentTenant, options)
    {
        _currentUser = currentUser;
        _serializer = serializer;
        _dbSettings = dbSettings.Value;
        _events = events;
    }

   // this gets called once per startup
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // SoftDelete FilterQuery gets set for each Entitiy
        modelBuilder.AppendGlobalQueryFilter<ISoftDelete>(s => s.DeletedOn == null);

        base.OnModelCreating(modelBuilder);

        modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
    }

    // this gets called for each Tenant
    // here would be a good place to disable to filter queries if the tenant is the root tenant.
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      
       ......

    }

....

}

Thank for your help!

spent already a lot of time trying to figure out what the cleanest way is to achiev this.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions