EntityFramework.Docs
EntityFramework.Docs copied to clipboard
How to use context in a query filter using IEntityTypeConfiguration
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.
- ID: b7c9bd3e-4b99-9acf-48d8-e47bc53eb54e
- Version Independent ID: 220e0a6c-d227-956a-d27a-7c88804f770c
- Content: Global Query Filters - EF Core
- Content Source: entity-framework/core/querying/filters.md
- Product: entity-framework
- GitHub Login: @anpete
- Microsoft Alias: divega
@timschwallie Please provide more details on what you are suggesting.
We don't have documentation about EntityTypeBuilder.
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);
}
}