Skip to content

Feature Request: Query Filter with DbContext as one of the arguments #825

Open
@Seramis

Description

@Seramis

I would like to propose a addition to the Query Filter capability of EntityFramework-Plus library.

This post is building upon examples in the docs: https://entityframework-plus.net/ef-core-query-filter

Let's take the following example:

// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();

ctx.Filter<Post>(q => q.Where(x => !x.IsSoftDeleted));

// SELECT * FROM Post WHERE IsSoftDeleted = false
var list = ctx.Posts.ToList();

If we would assume that the EntitiesContext has a public bool ExcludeDeleted { get; set; } then we would be able to set it and the following example could be possible:

// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
ctx.ExcludeDeleted = true;

ctx.Filter<Post>((q, dbCtx) => q.Where(x => !dbCtx.ExcludeDeleted || !x.IsSoftDeleted));

// SELECT * FROM Post WHERE IsSoftDeleted = false
var list = ctx.Posts.ToList();

A more valid example would be based on multi-tenant scenario, where the DbContext is created or hydrated with TenantId while supplied by the dependency injection before any queries would run. But for now i wanted to keep this example be based on existing documentation as much as possible.

Linq2Db supports filtering that optionally can take in DbContext along with the IQueryable: https://github.com/linq2db/linq2db/blob/f60cef6849291a4ff810adcb21175d2e2ec19f8c/Tests/Linq/Linq/QueryFilterTests.cs#L69

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions