Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Stored Procedures vs Generated SQL

I've used the entity framework in a couple of projects. In every project, I've used stored procedures mapped to the entities because of the well known benefits of stored procedures - security, maintainability, etc. However, 99% of the stored procedures are basic CRUD stored procedures. This seems like it negates one of the major, time saving features of the Entity Framework -- SQL generation.

I've read some of the arguments regarding stored procedures vs. generated SQL from the Entity Framework. While using CRUD SPs is better for security, and the SQL generated by EF is often more complex than necessary, does it really buy anything in terms of performance or maintainability to use SPs?

Here is what I believe:

  • Most of the time, modifying an SP requires updating the data model anyway. So, it isn't buying much in terms of maintainability.
  • For web applications, the connection to the database uses a single user ID specific to the application. So, users don't even have direct database access. That reduces the security benefit.
  • For a small application, the slightly decreased performance from using generated SQL probably wouldn't be much of an issue. For high volume, performance critical applications, would EF even be a wise choice? Plus, are the insert / update / delete statements generated by EF really that bad?
  • Sending every attribute to a stored procedure has it's own performance penalties, whereas the EF generated code only sends the attributes that were actually changed. When doing updates to large tables, the increased network traffic and overhead of updating all attributes probably negates the performance benefit of stored procedures.

With that said, my specific questions are:

Are my beliefs listed above correct? Is the idea of always using SPs something that is "old school" now that ORMs are gaining in popularity? In your experience, which is the better way to go with EF -- mapping SPs for all insert / update / deletes, or using EF generated SQL for CRUD operations and only using SPs for more complex stuff?

like image 768
DCNYAM Avatar asked Jul 22 '11 13:07

DCNYAM


1 Answers

I think always using SP's is somewhat old school. I used to code that way, and now do everything I can in EF generated code...and when I have a performance problem, or other special need, I then add back in a strategic SP to solve a particular problem....it doesn't have to be either or - use both.

All my basic CRUD operations are straight EF generated code - my web apps used to have 100's or more of SP's, now a typical one will have a dozen SP's and everything else is done in my C# code....and my productivity has gone WAY up by eliminating those 95% of CRUD stored procs.

like image 117
E.J. Brennan Avatar answered Sep 22 '22 19:09

E.J. Brennan