Using global query filters in Entity Framework Core

Download this .NET sample project showing global query filters in Entity Framework Core. The examples include soft deleting records and multitenancy.

You can find out more about global query filters in Entity Framework Core, or watch the video:

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

Software

This is the software that will need to be installed onto your machine.

Import the database

This application integrates with a SQL Server database. Inside the Database folder, there is RoundTheCode_EFQueryFilter.bacpac and RoundTheCode_EFQueryFilter.bak. Use either of these database backups to import the database to your local SQL Server database server.

The application

Inside Api/appsettings.Development.json, find ConnectionStringsEFQueryFilterDbContext and point the connection string to your imported database.

Open up RoundTheCode.EFQueryFilter.slnx, and run the application. The Swagger documentation will load at https://localhost:7289/swagger with these endpoints:

  • /api/products - Uses query filters to get all products from the SiteId set in ConnectionStringsEFQueryFilterDbContext that aren't deleted.

  • /api/products/active - Ignores the multitenancy filter to get products from all sites that aren't deleted

  • /api/products/{id} - Soft deletes a product by intercepting OnSaveChangesAsync. Set Id to 2 to try it.

Reset products data

To reset [dbo].[Products] , run ResetProducts.sql inside the Database folder against your database. This will revert [dbo].[Products] back to the original data.