-
Book Overview & Buying
-
Table Of Contents
Software Architecture with C# 10 and .NET 6 – Third Edition - Third Edition
By :
An interesting Entity Framework advanced feature that is worth mentioning is global filters, which were introduced at the end of 2017. They enable techniques such as soft delete and multi-tenant tables that are shared by several users, where each user just sees its records.
Global filters are defined with the modelBuilder object, which is available in the DbContext OnModelCreating method. The syntax for this method is as follows:
modelBuilder.Entity<MyEntity>().HasQueryFilter(m => <define filter condition here>);
For instance, if we add an IsDeleted property to our Package class, we may soft delete a Package without removing it from the database by defining the following filter:
modelBuilder.Entity<Package>().HasQueryFilter(m => !m.IsDeleted);
However, filters contain DbContext properties. Thus, for instance, if we add a CurrentUserID property to our DbContext subclass (whose value...