使用存储过程的多租户 EF 实施
在 EF4 中使用 SProcs 时,我了解将视图映射到实体,然后使用函数导入将我的设置/更新/删除映射到存储过程的概念。我的问题是这如何应用于多租户架构。考虑以下场景:
我们有数百个客户使用我们的多租户数据库/应用程序。每个客户在帐户表中都有 50-200 个帐户。如果我向 EF 公开视图,则无法参数化该视图。因此,根据定义,以下行
query = (from e in context.Accounts select e).where(e => e.companyID = 1)
[forgive me if I'm syntactically incorrect. still learning EF!]
:必须首先返回所有帐户,然后使用我的磨损子句进行过滤。这是正确的吗?我无法想象这个过程还会如何进行。
我在这里错过了什么吗?
When using SProcs in EF4, I understand the concept of mapping views to entities, then using function imports to map my set/update/deletes to sprocs. The question I have is how this applies to multi tenant architecture. Consider the following scenario:
We have several hundred customers utilizing our multi-tenant database/application. Each customer has somewhere between 50-200 Accounts in the Accounts table. If I expose a view to EF, I cannot parameterize that view. So the following line:
query = (from e in context.Accounts select e).where(e => e.companyID = 1)
[forgive me if I'm syntactically incorrect. still learning EF!]
,by definition, would have to return all of the Accounts first, then filter using my wear clause. is this correct? I can't imagine how else the process would work.
Am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是 Linq-To-Objects 和 Linq-To-Entities 之间的区别。 Linq-To-Objects 在
IEnumerable
上运行,并将委托传递给其方法来定义将在内存中执行的查询。 Linq-To-Entities 在IQueryable
上运行,并且您将表达式传递给其方法,并定义表达式树,该树由 Linq-to-entities 提供程序转换为另一种语法 - SQL!因此,您的查询将在数据库中执行,过滤也将在数据库中完成。请注意,在执行
AsEnumerable
、ToArray
、ToDictionary
或ToList
等命令后,您将查询的其余部分转换为Linq 到对象。如果您对存储过程执行的结果编写查询,那么您始终只执行 Linq-to-objects,仅查询 ObjectSet,直接形成 Linq-to-entities 查询。
That is the difference between Linq-To-Objects and Linq-To-Entities. Linq-To-Objects operates on
IEnumerable<T>
and you pass delegates to its methods to define the query which will be executed in the memory. Linq-To-Entities operates onIQueryable<T>
and you pass expressions to its methods do define expression tree which is transformed by Linq-to-entities provider into another syntax - to SQL!So your query will be executed in the database and filtering will be done in the database as well. Be aware that after executing commands like
AsEnumerable
,ToArray
,ToDictionary
orToList
you transform the rest of the query to Linq-to-objects.If you write the query on the result of stored procedure execution you are always doing Linq-to-objects only querying ObjectSets directly forms Linq-to-entities queries.
EF 不应该先恢复所有帐户,然后再进行过滤。相反,它应该发出带有 WHERE 子句的查询。
您可以使用 SQL Profiler 进行检查,以确保 100% 确定。
EF shouldn't be bringing all the accounts back first and then filtering. Rather, it should be be emitting a query with a WHERE clause.
You can check using SQL Profiler, just to be 100% sure.