EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

How to use context in a query filter using IEntityTypeConfiguration

Open timschwallie opened this issue 7 years ago • 3 comments

Probably should say some comments regarding issues with using the HasQueryFilter on the EntityTypeBuilder.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

timschwallie avatar Nov 14 '18 22:11 timschwallie

@timschwallie Please provide more details on what you are suggesting.

ajcvickers avatar Aug 29 '19 22:08 ajcvickers

We don't have documentation about EntityTypeBuilder.

divega avatar Sep 11 '19 17:09 divega

Closures with type matching the context used to execute query will be replaced with the current instance when running it. So the following pattern can be used to configure global query filters:

    private sealed class CustomerEntityConfiguration : IEntityTypeConfiguration<Customer>
    {
        private readonly SomeDbContext _context;
        public void Configure(EntityTypeBuilder<Customer> builder)
        {
            builder.HasQueryFilter(d => d.TenantId == _context.TenantId);
        }
    }

AndriySvyryd avatar Dec 19 '24 20:12 AndriySvyryd