微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 扩展方法中的表达式树将在本地进行评估

我想将TenantId添加到Asp.Net Identity表(例如:User).

以下代码段正常.租户上下文将通过DI注入,租户根据http上下文域进行更改:

private readonly ITenantContext<ApplicationTenant> tenantContext;

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,ITenantContext<ApplicationTenant> tenantContext) : base(options)
{
    this.tenantContext = tenantContext;
}

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<ApplicationUser>(b =>
    {
        // add tenant
        b.Property(typeof(int),"TenantId");
        b.HasQueryFilter(x => EF.Property<int>(x,"TenantId") == this.tenantContext.Tenant.Id);
    });
}

为了重用,我想为entityBuilder创建一个扩展方法

public static class EntityTypeBuilderExtensions
{

    public static void AddTenancy<TEntity>(
        this EntityTypeBuilder<TEntity> builder,Expression<Func<int>> tenantId,string propertyName = "TenantId")
        where TEntity : class
    {
        // validate
        Ensure.Argument.NotNull("builder",builder);

        // add property to entity
        builder.Property(typeof(int),propertyName).Isrequired();


        /* THIS WORKS BUT WILL BE EVALUATED LOCALLY */
        // left
        var parameterExp = Expression.Parameter(typeof(TEntity),"x"); // e = TEntity => e.g: User
        var propertyNameExp = Expression.Constant(propertyName,typeof(string)); // the name of the tenant column - eg.: TenantId
        // right
        var tTenantId = Expression.Convert(tenantId.Body,typeof(int));  // tenantId
        var propertyMethod = typeof(EF).getmethod(nameof(EF.Property),BindingFlags.Public | BindingFlags.Static).MakeGenericmethod(typeof(int)); // build EF.Property
        var propertyMethodExec = Expression.Call(propertyMethod,parameterExp,propertyNameExp);  // represents EF.Property(e,"TenantId")
        var bodyExp = Expression.Equal(propertyMethodExec,tTenantId);
        var lambda = Expression.Lambda(bodyExp,parameterExp);
        builder.HasQueryFilter(lambda);
    }
}

并在db上下文中:

private Func<int> tenantId => () =>
{
    // return tenant id
    if (this.tenantContext != null && this.tenantContext.Tenant != null)
    {
        return this.tenantContext.Tenant.Id;
    }

    return -1;
};

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<ApplicationUser>(b =>
    {
        b.AddTenancy(() => this.tenantId(),"TenantId");
    });
}

扩展方法也很好,但表达式在本地进行评估:-(.有人可以帮我修复它吗?

The LINQ expression ‘where (Property([x],“TenantId”) ==
Invoke(__ef_filter__tenantId_0))’ Could not be translated and will be
evaluated locally. The LINQ expression ‘where ([x].normalizedUserName
== __normalizedUserName_0)’ Could not be translated and will be evaluated locally. The LINQ expression ‘FirstOrDefault()’ Could not be
translated and will be evaluated locally.

解决方法

问题是这里的Func

private Func<int> tenantId => ...

这导致错误的翻译调用(__ ef_filter__tenantId_0))和客户评估.

解决方案是使tenantId简单地返回int属性方法.例如,保持通话

b.AddTenancy(() => this.tenantId(),"TenantId");

它应该改

private int tenantId()
{
    // return tenant id
    if (this.tenantContext != null && this.tenantContext.Tenant != null)
    {
        return this.tenantContext.Tenant.Id;
    }

    return -1;
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐