Есть ли способ универсально добавить предложение where ко всем запросам, которые запускает EF? Я хочу, чтобы все запросы, чтобы работать с «entity.Active == правда», напримерEF 6 - Inject where where
0
A
ответ
0
Самый простой способ (без реализации интерфейса QueryTranslator) является создание метода расширения для контекста, как этот
public static IQyertable<YourEntity> OnlyActiveEntities(this YourDbContext context, Action<DbSet<YourEntity>> setConfigurator = null)
{
var dbSet = context.Set<YourEntity>();
setConfigurator?.Invoke(dbSet);
return context.Set<YourEntity>().AsQueriable().Where(entity => entity.Active == true);
}
1
Некоторые третья сторона библиотека позволяет фильтровать запросы: Entity Framework Filter Library List
Отказ от ответственности: Я владелец проекта Entity Framework Plus
Wiki: EF + Query Фильтр
Эта функция - именно то, что вы ищете. Вы можете добавить глобальный фильтр для фильтрации всех запросов.
Пример:
// using Z.EntityFramework.Plus; // Don't forget to include this.
QueryFilterManager.Filter<ISoftDelete>(q => q.Where(x => x.IsActive));
Смежные вопросы
- 1. EF include with where clause
- 2. Nullable DateTime in Where Where in EF Linq Query
- 3. Linq to EF guid в where where всегда возвращает null
- 4. Entity Framework 6 литой объект наследования при выполнении where where
- 5. sqlAlchemy dynamic where where
- 6. SQL Server: case when where where where
- 7. SQL - «IF» в Where Where
- 8. Парализовать параметры where where?
- 9. Laravel where where
- 10. NullReferenceException using where where
- 11. ZF2 SQL Where where
- 12. where where with timezones
- 13. Lambda expression Where where
- 14. Array in Where where
- 15. Enityframework - ConcurrencyMode where where
- 16. LINQ Join Where Where
- 17. conditional where where
- 18. mysql shrink where where
- 19. Datediff in where where
- 20. «case» внутри where where
- 21. Linq Subquery Where Where
- 22. SQL Optimization where where
- 23. Sparql where where
- 24. IQueryable where where
- 25. laravel Eloquent where where
- 26. MySQL where where with microseconds
- 27. Как использовать предложение where в MVC ef
- 28. Dynamic EF Where Clause raise ArgumentNullException
- 29. EF 4 Dynamic Where Clause не работает
- 30. MSSQL aggregate игнорирует where where