EF动态过滤器表达式

发布于 2025-02-13 05:58:11 字数 1089 浏览 4 评论 0原文

我们正在使用EF动态过滤器(ASP.NET MVC 5应用程序) https://entityframeframeworkerwork-dynamicfilters.net/overview

下面的过滤器只能设置一次。

builder.Filter("IMultiOrganization", (IMultOrganization d) => ((d.TenantId == GlobalAppVariables.IssuerId) && (d.IsShared == true)) || ((d.IsShared == false && d.AzureObjIdentifier == GlobalAppVariables.AzureObjIdentifier)));

变量“ issuerId”和“ azureobjistientifier”是动态的会话变量,这些变量一直在改变。这会导致问题,我希望过滤器可以直接使用这些变量。

根据文档,这是因为该文件不是代表表达式引起的。

可以通过提供特定值来定义特定实体类或接口上的过滤器,例如在IsoftDelete接口上创建的ISDEATED过滤器,该过滤器将通过应用“ ISDETERTED == false”的条件来自动过滤这些实体。

滤波器值也可以通过委托/表达式而不是特定值提供,该值允许您动态更改参数值。例如,可以在用户ID上创建过滤器并根据HTTP请求提供。

我们还使用委托过滤器确实有效。

builder.EnableFilter("IMultiTenant", () => GlobalAppVariables.AzureObjIdentifier != null || GlobalAppVariables.IssuerId != Guid.Empty);

但是我无法将第一个过滤器作为代表表达式工作,并且需要一些帮助。

We are using EF Dynamic filters (ASP.NET MVC 5 app)
https://entityframework-dynamicfilters.net/overview

The filter below is working, but set only once.

builder.Filter("IMultiOrganization", (IMultOrganization d) => ((d.TenantId == GlobalAppVariables.IssuerId) && (d.IsShared == true)) || ((d.IsShared == false && d.AzureObjIdentifier == GlobalAppVariables.AzureObjIdentifier)));

The variables 'IssuerId' and 'AzureObjIdentifier' are dynamic session variables, those are changing all the time. This causes problems and I would like the filter to use those variables straight from the session.

According to the documentation this is caused because this filer isn't a delegate expression.

Filters can be defined on a specific entity class or an interface by providing a specific value, e.g. an IsDeleted filter created on an ISoftDelete interface which will automatically filter those entities by applying the condition "IsDeleted==false".

Filter values can also be provided via a delegate/expression instead of a specific value which allows you to change the parameter value dynamically. For example, a filter can be created on the UserID and provided per HTTP request.

We also use delegate filters what indeed is working fine.

builder.EnableFilter("IMultiTenant", () => GlobalAppVariables.AzureObjIdentifier != null || GlobalAppVariables.IssuerId != Guid.Empty);

But I can't get the first filter work as a delegate expression and need a bit help on that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2025-02-20 05:58:11

我通过在过滤器中使用参数找到解决方案。

首先,我更改了现在支持参数的过滤器。

builder.Filter("IMultiOrganization", (IMultiOrganization d, Guid tenantId, string azureId) => (d.TenantId == tenantId && d.IsShared == true) || (d.AzureObjIdentifier == azureId && d.IsShared == false), GlobalAppVariables.IssuerId, GlobalAppVariables.AzureObjIdentifier);

然后,我在DB上下文中调用下面的方法构造函数

private void SetMultiOrganizationFilterParams()
    {
        if (GetFilterEnabled("IMultiOrganization"))
        {
            this.SetFilterScopedParameterValue("IMultiOrganization", "azureId", GlobalAppVariables.AzureObjIdentifier);
            this.SetFilterScopedParameterValue("IMultiOrganization", "tenantId", GlobalAppVariables.IssuerId);
        }
    }

来源: https://github.com/zzzprojects/entityframework.dynamicfilters#changing-filter-parameter-values

I found the solution by using parameters within the filter.

First of all I changed the filter which now supports parameters.

builder.Filter("IMultiOrganization", (IMultiOrganization d, Guid tenantId, string azureId) => (d.TenantId == tenantId && d.IsShared == true) || (d.AzureObjIdentifier == azureId && d.IsShared == false), GlobalAppVariables.IssuerId, GlobalAppVariables.AzureObjIdentifier);

Then I call method below in the db context constructor

private void SetMultiOrganizationFilterParams()
    {
        if (GetFilterEnabled("IMultiOrganization"))
        {
            this.SetFilterScopedParameterValue("IMultiOrganization", "azureId", GlobalAppVariables.AzureObjIdentifier);
            this.SetFilterScopedParameterValue("IMultiOrganization", "tenantId", GlobalAppVariables.IssuerId);
        }
    }

Source: https://github.com/zzzprojects/EntityFramework.DynamicFilters#changing-filter-parameter-values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文