多租户架构实体框架 - 由租户ID的滤波单表 [英] Entity Framework for Multi-tenant architecture - filterings single table by tenant ID
问题描述
我们正在寻找在实体框架租户ID自动过滤所有CRUD操作的方式。
我们以为的想法是:
- 使用表值用户自定义函数
- 使用存储过程(但我们真的不希望,因为我们使用的ORM,以避免这样做)
- 一些如何修改用于生成SQL模板添加一个,在各声明的条款。
- 一些如何修改用于生成LINQ中的控制器(我们可能会使用MVC)中的模板。
任何提示?
-Thanks
亚历克斯。
使用表值用户自定义函数
块引用>表值函数只能在.NET 4.5 Beta版提供(在code不可用在前)。使用它们仍然不会帮你,因为你将不得不使用该函数在每一个LINQ查询所以它与使用where子句。
使用存储过程(但我们真的不希望,因为我们使用的ORM,以避免这样做)
块引用>它可以对一些特殊复杂的查询有用,但通常这不是你想要的。
一些如何修改用于生成SQL模板添加一个where子句上每条语句。
块引用>太复杂,完全不同的抽象级别。
一些如何修改用于生成在控制器LINQ(我们可能会使用MVC)的模板。
块引用>接近理想的解决方案。您只需换到你的实体设置为若干code访问,这将是这样的:
公共类MultiTenantAccess< T>其中T:IMultitenant
{
私人IDbSet< T>组; ... 公众的IQueryable< T> GetQuery(INT tenantID)
{
返回set.Where(E => e.TenantID == tenantID);
}
}有时候,这是核心的东西,所谓的通用信息库,但它实际只是EF组的包装。你总是会使用
GetQuery
来查询,而不是使用您的数据存储DbSet
直接We are looking for a way of automatically filtering all CRUD operations by a tenant ID in Entity Framework.
The ideas we thought of were:
- Using table valued user defined functions
- Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)
- Some how modifying the templates used to generate the SQL to add a where clause on each statement.
- Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).
Any tips?
-thanks Alex.
解决方案Using table valued user defined functions
Table valued function are only available in .NET 4.5 Beta (and not available in code first). Using them will still not help you because you will have to use the function in every LINQ query so it is the same as using where clause.
Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)
It can be useful for some special complex queries but generally it is not what you want.
Some how modifying the templates used to generate the SQL to add a where clause on each statement.
Too complex and on completely different level of abstraction.
Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).
Close to ideal solution. You simply need to wrap access to your entity set into some code which will look like:
public class MultiTenantAccess<T> where T : IMultitenant { private IDbSet<T> set; ... public IQueryable<T> GetQuery(int tenantID) { return set.Where(e => e.TenantID == tenantID); } }
Sometimes this is core for something called Generic repository but it is really just a wrapper around EF set. You will always use
GetQuery
to query your data store instead of usingDbSet
directly.这篇关于多租户架构实体框架 - 由租户ID的滤波单表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!