I have this query filter on tenant:
m.Builder.HasQueryFilter(mm => EF.Property<int>(mm, "TenantId") == SindikatDataSetBase.TenantId);
When I login into a desktop app as a different user static variable SindikatDataSetBase.TenantId changes, but the query filter still uses the previous TenantId value.
Well, it is bad to use static variable. EF Core supports variables in filters if they are part of DbContext
Create property in your DbContext
TenantId and initialize, for example in constructor or using default initialization.
public class MyDbContext: DbContext
{
public int TenantId { get; set; } = SindikatDataSetBase.TenantId;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeEntity>()
.HasQueryFilter(mm => EF.Property<int>(mm, "TenantId") == TenantId);
}
}
After that each new MyDbContext
will catch your static variable and EF can create correct query.
UPDATE
How to avoid configuring parametrized QueryFilter inside DbContext
Idea is simple - mimic that we are already in DbContext's metod.
MyDbContext ctx = null;
m.Builder.HasQueryFilter(mm => EF.Property<int>(mm, "TenantId") == ctx.TenantId);