0

我有一个多租户 ASP.NET 应用程序,我们的数据库设置了软删除。最初,我们直接在查询级别处理数据限制,例如:

var foos = context.Foos.Where(foo => !foo.Deleted && foo.TenantId = currentTenantId).ToList();

可以想象,这会使我们的数据访问层中的所有查询膨胀,如果忘记添加正确的过滤条件,这会使 API 非常容易受到攻击。我们决定将全局过滤应用于上下文Z.EntityFramework.Plus.EF6

public class FooDataContextFactory
{
    public FooDataContext CreateContext()
    {
        var context = new FooDataContext();

        context.Filter<Foo>(collection => collection.Where(foo=> !foo.Deleted));

        var principal = Thread.CurrentPrincipal as ClaimsPrincipal;
        if (principal.HasClaim(claim => claim.Type == "TenantId"))
        {
            var currentTenantId = int.Parse(principal.FindFirst("TenantId").Value);
            context.Filter<Foo>(collection => collection.Where(foo => foo.TenantId == currentTenantId));
        }

        return context;
    }
}

这非常适合单个用户。但是,当您切换租户时,我们会遇到将过滤器表达式保存在查询计划缓存中的问题。这是 Entity Framework Plus 的一个已知问题,由于它似乎没有得到解决,我需要找到一种解决方法。

我能想到的最直接的解决方案是将查询计划缓存的生命周期与当前会话相关联,当用户注销或切换租户时,缓存被销毁。这是可能的,如果是这样,我怎样才能做到这一点?

4

1 回答 1

0

我遇到了同样的问题,并尝试使用 Z.EntityFramework.Plus.EF6 解决同样的问题。我发现 zzzprojects 团队也有EntityFramework.DynamicFilters可以更好地实现此目的。缓存的查询是参数化的,并且在运行时使用您提供的选择器函数注入值。

using System.Data.Entity;
using EntityFramework.DynamicFilters;

public class Program
{   
    public class CustomContext : DbContext
    {
        private int _tenantId;

        public int GetTenantId()
        {
            return _tenantId;
        }

        // Call this function to set the tenant once authentication is complete.
        // Alternatively, you could pass tenantId in when constructing CustomContext if you already know it
        // or pass in a function that returns the tenant to the constructor and call it here.
        public void SetTenantId(int tenantId)
        {
            _tenantId = tenantId;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Filter applies to any model that implements ITenantRestrictedObject
            modelBuilder.Filter(
                "TenantFilter",
                (ITenantRestrictedObject t, int tenantId) => t.TenantId == tenantId,
                (CustomContext ctx) => ctx.GetTenantId(), // Might could replace this with a property accessor... I haven't tried it
                opt => opt.ApplyToChildProperties(false)
            );
        }
    }

    public interface ITenantRestrictedObject
    {
        int TenantId { get; }
    }
}
于 2018-08-24T01:36:13.333 回答