Entity SQL 还是 StoredProcedure,该用什么?

发布于 2024-11-09 12:15:07 字数 191 浏览 0 评论 0原文

我可以选择在 DAL 中使用Entity SQL 查询,或者在 db 级别使用存储过程作为选项。

我应该使用哪一个。这两种方法的优点/缺点是什么?

我更倾向于Entity SQL,因为我不希望我的逻辑以任何形式的数据库级别暴露。

I have got choice for using either Entity SQL query inside my DAL or use Stored Procedure at db level as a option.

Which one should I use. And what are the benefits/drawbacks for either approach?

I am more inclined towards Entity SQL as I don't want my any form of db level exposure of my logic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2024-11-16 12:15:07

根据我的经验:

实体框架:

  • 逻辑保留在应用程序中
  • 更容易与源代码控制集成(根据我的经验)
  • 如果需要,还可以使用存储过程(我在全文搜索例程中使用它)
  • 与数据库无关 ->好吧,我之前没有真正尝试过更改数据库,但它应该可以保护您免受底层持久性存储的影响。
  • 个人偏好:获取对象/模型数组而不是庞大的数据表更加漂亮和方便..更不用说那我
    需要知道列的顺序等等......

数据库 SP

  • 您不必学习任何新东西 - 有些任务对于存储过程来说是微不足道的,但在使用 EF 时可能会非常令人烦恼地晦涩难懂。好吧,这取决于你追赶的速度。
  • 易于使用,更精细的控制 - 到目前为止,我不知道如何创建一个在实体的 linq 中使用 WITH 和 OVER() 的查询。

就我个人而言,我也会使用 EF,因为我已经上面提到的(加上我现在想不起来的其他一些)。此外,如果我无法使用 linq 查询执行或无法快速执行某些操作,我将仅创建一个存储过程或仅执行一条 SQL 语句(是的,您可以使用上下文执行 SQL)。

Based on my experience:

Entity Framework:

  • Logic stays in the application
  • Easier to integrate with source control (in my experience)
  • Can also use stored procedures if needed (I used it in a full text search routine)
  • Database agnostic -> well, I haven't really tried changing db's before, but its supposed to shield you from the underlying persistence store
  • Personal Preference: its so much prettier and convenient to get an array of objects/models instead of a bulky datable.. not to mention that I
    need to know the order of the columns and such..

Database SPs

  • You don't have to learn anything new - There are certain tasks that are trivial from a storedproc but can be so annoyingly obscure when using EF. Well, it depends on how quick you catch up.
  • Easy to use, more granular control - up til now, I don't know how to create a query that uses WITH and OVER() in linq to entities..

Personally, I'd go with EF too for the reasons I've mentioned above (plus a few others I cant think of right now). Besides, if there's something I can't do or cant do fast with a linq query, I'll just create a stored proc or just execute an SQL statement (yes you can execute SQL using the context).

半仙 2024-11-16 12:15:07

如果您也是数据库管理员,请使用 LINQ 实体。
如果将代码放在一起会更容易。

但是,如果您熟悉 SQL(或者其他人负责 SQL Server 性能),则可以使用存储过程。无需重新发布应用程序即可更轻松地优化数据库操作。

If you are the DB administrator as well, then use LINQ Entities.
It's easier if you keep your code together.

However, if you know your way around SQL (or someone else is in charge of SQL Server Performance), then use Stored Procedures. It's much easier to optimize your DB operations without re-releasing your application.

如果没有你 2024-11-16 12:15:07

两者都有自己的优点和缺点。

Entity SQL 始终参数化,因此可以免受 SQL 注入攻击。
但如果您足够了解 SQL,您会发现存储过程会更加谨慎。

您知道可以在实体框架中使用存储过程吗?
更愿意建议实体框架。当您觉得在特定场景中存储过程更加优化时,请使用存储过程。

Both are having its own pros and cons.

Entity SQL is secure from SQL injection attacks as its always parameterized.
But if you know SQL well enough, you would see cases where a Stored Procedure would be more prudent.

Do you know that you can use Stored Procedures in Entity Framework?
Would prefer suggesting Entity Framework. Use Stored Procedures when you feel in a particular scenario Stored Procedures are more optimized.

梦情居士 2024-11-16 12:15:07

我仅在以下情况下使用 StoredProc:

  1. 为了获取一些数据,您应该向数据库发出超过 2 个请求 - 为什么要从脚本发送它,编写一个过程会更容易,该过程将完成数据库中的所有操作,并且只向您发送结果(它太快得多)
  2. 如果编写一个过程并花费 5 分钟比编写一个天才实体花费 1 天更简单
  3. 如果某些操作默认在数据库中运行得更快或不受实体支持

I use StoredProc only if:

  1. For get some data you should made more than 2 requests to database - why to sent it from script, it's easer to write a proc that will done all action in db and only sent to you the resalts (its too much faster)
  2. If simplier to write a proc and spend 5 mins than a genious entities and spend 1 day
  3. If some operations by defaults runnig faster in db or not supported by entities
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文