Deploy Global .NET + Postgres Apps Without Cloud Complexity (Sponsored)
Running a .NET app in the cloud shouldn't feel hard. But many teams still deal with region setup, config files, and ongoing ops just to stay online. Fly.io removes that friction and lets you focus on shipping features.
Fly.io runs your ASP .NET apps close to users in 35 global regions, giving fast response times and a smooth user experience worldwide. It's built for stateful .NET apps, so your API, frontend, and database can live together instead of being split across services.
With Fly.io Managed Postgres, you get automatic backups, high availability, scaling, monitoring, and encryption out of the box. No database babysitting. No late-night alerts. Just Postgres that works in production.
Pricing is predictable, and small instances make it easy to start with side projects and grow when traffic picks up. If you want simpler deploys, global reach, and less ops work for your .NET + Postgres apps, Fly.io is a solid choice.
Global query filters in Entity Framework Core (EF Core) is a powerful feature that can be effectively used to manage data access patterns.
Global query filters are LINQ query predicates applied to EF Core entity models. These filters are automatically applied to all queries that involve the corresponding entities.
This is especially useful in multi-tenant applications or scenarios requiring soft deletion.
In EF Core 10, Global Query Filters were renamed to Named Query Filters.
This update now solves a significant limitation EF Core previously had: support for multiple global query filters on a single entity.
Let's explore a use case where global query filters are particularly useful - entity soft deletion.
In some applications, entities can't be completely deleted from the database and should remain for statistics and historical purposes.
Or to ensure that related data remains unchanged, i.e, referenced by foreign keys. A solution for this use case is soft deletion.
Soft deletion is implemented by adding an is_deleted column to the database table for required entities.
Whenever an entity is considered deleted, this column is set to true.
In most application database queries, "deleted" entities should be ignored in read operations and not be visible to end users.
Let's explore an example for the following entities:
csharp
1publicclassAuthor2{3public required Guid Id {get;set;}4public required string Name {get;set;}5public required string Country {get;set;}6public required List<Book> Books {get;set;}=[];7}89publicclassBook10{11public required Guid Id {get;set;}12public required string Title {get;set;}13public required int Year {get;set;}14public required bool IsDeleted {get;set;}15public required Guid TenantId {get;set;}16public required Author Author {get;set;}17}
We need to create and set up our DbContext with a global query filter:
Here we are filtering out all softly deleted books from the result query. When querying books from DbContext, this query filter is applied automatically.
Let's have a look at the following minimal API endpoint:
Every time we query books, we only get those that are not deleted, thus we don't need to use a LINQ Where statement in all DbContext queries.
In some cases, however, we might need to access all entities and ignore the query filter. EF Core has a special method called IgnoreQueryFilters for such a case:
Another practical use case for global query filters is multi-tenancy.
A multi-tenant application shares a single software instance across multiple customers.
All customer data should not be visible to other customers.
Let's explore the simplest implementation of multi-tenancy: storing all data in a single database and table.
First, we need to add a TenantId property to the Books entity:
csharp
1publicclassBook2{3// Other properties ...45public required Guid TenantId {get;set;}6}
In this code, we've updated the query filter and added an x.TenantId == _currentTenantId statement.
Since we're creating a DbContext per request, we can inject the current tenant id from the request (the customer identifier accessing data in our application).
Here's a simple tenant service implementation that retrieves a tenant id from HTTP request headers:
On every read query, a tenant ID global query filter is applied to ensure data integrity.
As a result, when calling this endpoint, each customer can only retrieve their own books.
Global query filters in EF Core is a powerful feature that enforces data access rules consistently across the application.
They are particularly useful in multi-tenant architectures and in scenarios such as soft deletion, ensuring that filter queries are automatically applied to all read operations.
By applying these filters to EF Core entity models, you can significantly simplify your data access code, ensure data integrity and reduce the risk of forgetting to apply important filters to the read operations.
In EF Core 10, they got a nice update: now multiple query filters can be applied to the same entity.
You need to name the filters and ignore some of them when needed.
Hope you find this newsletter useful. See you next time.
You can download source code for this newsletter for free
The .NET Senior Playbook — 800+ real-world interview questions with expert answers across 50 chapters. You try to answer each question first, then reveal the full solution — and a test after every chapter proves it actually stuck. Finish, and you earn a verifiable certificate for your LinkedIn.