I want to build a custom query for an entity where I need to use a custom join to make the filter work right. I can not use a navigation property. Can EF Core do that, and how do i have to build the Global query filter. I do not know how I have to specify that join within parameters of the HasQueryFilter() Method
Global query filters are predicate expressions which generate automatic Where
clause. Hence they cannot contain explicit joins to the filtered entity table. They can introduce implicit joins though via navigation properties.
Shortly, if you can't use navigation properties, then you cannot use direct joins.
But you can use correlated subqueries. For instance:
public class EntityInfo
{
public int Id { get; set; }
public string EntityType { get; set; }
public int EntityId { get; set; }
public string Info { get; set; }
}
public class Foo
{
public int Id { get; set; }
public string Info { get; set; }
}
and then inside OnModelCreating
:
modelBuilder.Entity<Foo>().HasQueryFilter(e =>
this.Set<EntityInfo>().Any(ei => ei.EntityType == "Foo" && ei.EntityId == e.Id));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With