简体   繁体   English

使用C#+ Entity Framework +存储过程构造动态查询

[英]Constructing dynamic queries with c# + Entity Framework + Stored procedure

I am working on a piece of functionality where a user may select multiple parameters with multiple values in each parameter. 我正在开发一项功能,用户可以选择多个参数,每个参数中都包含多个值。 I am trying to figure out a way to design this functionality in my application using C#, entity framework with entities being mapped to stored procedure. 我试图找出一种使用C#在实体应用程序中设计此功能的方法,该实体框架将实体映射到存储过程。 Due to security reasons, my application has to access the database via a surrogate database which has only stored procedures. 由于安全原因,我的应用程序必须通过仅具有存储过程的代理数据库访问数据库。 Therefore my entities are mapped to stored procedures for insert, update, and select. 因此,我的实体被映射到存储过程以进行插入,更新和选择。 Ultimately, I need to pass the filters chosen by the user to the stored procedure for querying the database. 最终,我需要将用户选择的过滤器传递给存储过程以查询数据库。 One of the solutions I thought of is to retrieve all the data to my business layer and use linq to filter out further. 我想到的解决方案之一是将所有数据检索到我的业务层,并使用linq进一步过滤。 But this is not ideal due to the amount of data being filtered in the memory rater than in the database which is more better suited to do this kind of complex query. 但这是不理想的,因为在内存评估器中比在数据库中过滤的数据量更大,数据库更适合于执行这种复杂的查询。 I have seen posts for constructing dynamic queries with linq, but in these kind of posts, the entities are mapped to the tables which makes it easier. 我已经看到了使用linq构建动态查询的帖子,但是在这类帖子中,实体映射到了表,这使得操作起来更加容易。 Any help here will be greatly appreciated. 在这里的任何帮助将不胜感激。 Thank You, sirkal 谢谢你,sirkal

EF (and LINQ for that matter) use deferred execution. EF(和LINQ)使用延迟执行。 You can fairly easily create a dynamic query using IQueryable (Google search time?) and creating the filter criteria in an object you build (you can do it without the object, but think reusable). 您可以使用IQueryable(Google搜索时间?)相当轻松地创建动态查询,并在您构建的对象中创建过滤条件(您可以在没有对象的情况下进行操作,但认为可重用)。

As for the SQL sproc, you can also solve it there by passing in all of the items that can possibly change the filter and having SQL dynamically work on the data to produce a result set. 对于SQL sproc,您还可以在其中解决所有可能更改过滤器的项目,并让SQL动态处理数据以生成结果集,从而解决该问题。

Which to choose? 选择哪个? It really depends on where the core competency is in your group. 这实际上取决于您团队中的核心能力。 I prefer C# code mostly due to familiarity (spent years doing sprocs, but dynamic sprocs can be a royal pain). 我之所以喜欢C#代码,主要是因为熟悉(花数年时间从事sproc,但是动态sproc可能会带来麻烦)。

Now, one thing you do want to be wary of is ending up with dynamic queries that cannot easily be tuned by the server (like statistics in SQL Server, although other RDBMSes use similar concepts). 现在,您要警惕的一件事是结束于服务器无法轻松调整的动态查询(例如SQL Server中的统计信息,尽管其他RDBMS使用类似的概念)。 One issue I have seen with LINQ to SQL, for example, is dynamic queries that cause SQL to perform less than optimally, requiring a lot of handholding from the DBA. 例如,我在LINQ to SQL上看到的一个问题是动态查询,该查询导致SQL的执行不达最佳状态,需要DBA的大量支持。

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

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