public partial class AppDbContext(DbContextOptions options, ILegalEntityProvider legalEntityProvider) : DbContext(options)
{
public Guid? LegalEntityId { get; } = legalEntityProvider.LegalEntity;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
// Apply a global query filter to all entities implementing IMultiLegalEntity.
// This ensures that queries only return records associated with the current LegalEntityId,
// which is determined from the 'extension_LegalEntityId' claim in the user's identity.
// The LegalEntityId is retrieved through the LegalEntityProvider, and only records with
// matching LegalEntityIds will be included in the query results.
// Global query filters are compiled and cannot be “switched off” on the fly unless you design them to be conditional.
modelBuilder.SetQueryFilterOnAllEntities(
x => LegalEntityId.HasValue && x.LegalEntities.Any(le => le.Id == LegalEntityId.Value)
);
}
}
Я использую EF Core Global Filters для фильтров для фильтрации записей на основе LegaletityId . По сути, если в токене JWT есть LegalentityID, фильтр гарантирует, что возвращаются только записи, связанные с текущим LegatentityID. LegalentityID извлекается у поставщика и применяется в глобальном фильтре запроса, как указано выше. Поскольку глобальные фильтры запросов в EF Core собираются один раз во время запуска, они не динамически приспосабливаются к изменениям стоимости LegalentityID во время выполнения. Это происходит потому, что фильтр оценивается при создании модели и остается статичным для последующих запросов. Я не мог придумать решения. Есть идеи, как справиться с этим?
// Apply a global query filter to all entities implementing IMultiLegalEntity. // This ensures that queries only return records associated with the current LegalEntityId, // which is determined from the 'extension_LegalEntityId' claim in the user's identity. // The LegalEntityId is retrieved through the LegalEntityProvider, and only records with // matching LegalEntityIds will be included in the query results.
// Global query filters are compiled and cannot be “switched off” on the fly unless you design them to be conditional. modelBuilder.SetQueryFilterOnAllEntities( x => LegalEntityId.HasValue && x.LegalEntities.Any(le => le.Id == LegalEntityId.Value) ); } } [/code] Я использую EF Core Global Filters для фильтров для фильтрации записей на основе LegaletityId . По сути, если в токене JWT есть LegalentityID, фильтр гарантирует, что возвращаются только записи, связанные с текущим LegatentityID. LegalentityID извлекается у поставщика и применяется в глобальном фильтре запроса, как указано выше. Поскольку глобальные фильтры запросов в EF Core собираются один раз во время запуска, они не динамически приспосабливаются к изменениям стоимости LegalentityID во время выполнения. Это происходит потому, что фильтр оценивается при создании модели и остается статичным для последующих запросов. Я не мог придумать решения. Есть идеи, как справиться с этим?