I need to create a custom filter to filter my entity based on BranchId
. The BranchId
will be received as a List<long>
from the request headers or parameters. Here's the approach I followed:
Created IBranchEntity
Interface:
Domain.Shared
folder.BranchId
property, which I inherited in the relevant entity.Defined IBranchContext
Interface:
Entity Framework Filtering Logic:
Service Registration:
Registered the IBranchContext
in the HostModule.cs
under the ConfigureServices
method as follows:
context.Services.AddHttpContextAccessor(); context.Services.AddScoped<IBranchContext, BranchContext>();
Database Context Changes:
ClinicServiceDbContext
constructor to accept the IBranchContext
.
private readonly IBranchContext _branchContext;
public ClinicServiceDbContext(
DbContextOptions<ClinicServiceDbContext> options,
IBranchContext branchContext
) : base(options)
{
_branchContext = branchContext;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ConfigureClinicService();
builder.ConfigureBranchFilterForEntities(_branchContext);
}
After implementing the above, the filter is correctly identifying entities with the BranchFilter
when the application is initially loaded. However, it is not being triggered for subsequent requests.