简体   繁体   English

带存储过程的多租户EF实施

[英]Multi tenant EF implementation w/ Stored Procedures

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. 在EF4中使用SProcs时,我了解将视图映射到实体,然后使用函数导入将我的设置/更新/删除映射到proc的概念。 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. 每个客户在“帐户”表中都有50-200个帐户。 If I expose a view to EF, I cannot parameterize that view. 如果将视图公开给EF,则无法参数化该视图。 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. ,根据定义,必须先返回所有帐户,然后使用我的wear子句进行过滤。 is this correct? 这个对吗? I can't imagine how else the process would work. 我无法想象该流程还会如何运作。

Am I missing something here? 我在这里想念什么吗?

That is the difference between Linq-To-Objects and Linq-To-Entities. 那就是Linq-To-Objects和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-Objects在IEnumerable<T> ,您将委托传递给它的方法以定义将在内存中执行的查询。 Linq-To-Entities operates on IQueryable<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! Linq-To-Entities在IQueryable<T> ,您将表达式传递给它的方法时,会定义表达式树,该树由Linq-to-entities提供程序转换为另一种语法-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 or ToList you transform the rest of the query to Linq-to-objects. 请注意,执行ToDictionary诸如AsEnumerableToArrayToDictionaryToList类的命令后,您ToDictionary查询的其余部分转换为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. 如果您对存储过程执行的结果写查询,那么您总是在执行Linq-to-objects,而只有查询ObjectSet直接形成Linq-to-entities查询。

EF shouldn't be bringing all the accounts back first and then filtering. EF不应先撤回所有帐户,然后再进行过滤。 Rather, it should be be emitting a query with a WHERE clause. 相反,它应该发出带有WHERE子句的查询。

You can check using SQL Profiler, just to be 100% sure. 您可以使用SQL事件探查器进行检查,只是要百分百确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM