忽略联接实体的全局查询过滤器 [英] Ignore global query filter for joined entities

查看:93
本文介绍了忽略联接实体的全局查询过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现租户和软删除功能时,全局查询过滤器非常方便.

Global query filters are very handy when implementing tenant and soft deletion features.

但是我的问题是,例如,当我编写带有联接的查询时

But my problem is is that when i write a query with joins, for instance

dbContext
    .entity1
    .Include("entity2.entity3.entity4")
    .Where(something)
    .select(something)
    .toList();

并且这些实体中的每个实体都有全局过滤器,然后在生成的SQL中,我为每个JOIN获得一个完整的子查询,在其中它选择该实体的所有字段并检查全局过滤器.

and every one of those entities has global filters, then in the generated SQL i get for every JOIN a full Subquery where it selects all fields of that entity and checks for the global filters.

但是我不想要那个.我希望全局过滤器仅适用于查询的根实体(entity1),而所有其他实体都可以正常加入.

But i dont want that. I want the global filters to only apply to the root entity of the query (entity1), and all the other entities to join normaly.

btw实体之间的关系是:

btw the relationships of the entities are:

  • 1个实体4->N实体3
  • 1个实体3->N实体2
  • 1个实体2->N个实体1

在我的情况下,每个实体都会得到其承租人".字段集,当软删除实体时,该软删除将级联到其所有子级和子级.因此,检查每个联接的这些字段完全是浪费时间.

In my case, every entity gets its "tenant" field set and when soft deleting an entity, that soft deletion is cascaded to all its children and subchildren. Because of that checking those fields for every join is a complete waste of time.

推荐答案

目前,您不能忽略 Include 中的查询过滤器.EF核心存储库中有一个一个问题,但它不会改进查询过滤器不要进入EF Core 5.

At the moment you can't ignore query filter in an Include. There is an issue on the EF Core repo about improving query filters, but it won't go into EF Core 5.

一种可能有用的方法是运行多个查询,并依靠EF Core的关系修正功能将事物结合在一起(我在文章

One approach that might help is running multiple queries and rely on EF Core's relational fixup feature to join things together (I explain relational fixup in my article EF Core In depth – what happens when EF Core reads from the database?).

这里是执行两个查询的示例,其中关系修正将结合在一起.

Here is an example of doing two queries, where relational fixup will join together.

var mainEntities = dbContext.entity1
   .Where(something)
   .Select(something) //But must contain the ent1 primary key, e.g. Id
   .ToList();
var ent2list = dbContext.entity2.IgnoreQueryFilters()
   .Include("entity3.entity4")
   .Where(ent2 => mainEntities.Select(ent1 => ent1.Id).Contains(ent2.Id)
   .ToList();

在此EF Core的关系修正结束时,将在相应的entity1实例中填充entity2导航属性.注意:如果使用 AsNoTracking ,则关系修正无法正常工作.

At the end of this EF Core's relational fixup will fill in the entity2 navigational property in the appropriate entity1 instance. NOTE: Relational fixup doesn't work if you use AsNoTracking.

这篇关于忽略联接实体的全局查询过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆