Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are SPs redundant if using Linq and EF (best practice)

I'm thinking about moving my development team to LINQ and the Entity framework. If I do, should I be thinking about removing SPs?

Typically our architecture is (in order);

SQL -> SPs -> Data Access Layer -> Business Objects -> GUI

Should I be moving to something similar to:

SQL -> Entity Framework Layer -> Business Objects (probably inherited from EF layer) -> GUI

Will I see as much benefit if I leave SPs in? Also what are the best practices?

like image 917
Mike Mengell Avatar asked Dec 23 '10 10:12

Mike Mengell


People also ask

Is Linq better than stored procedure?

Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure.

Which is faster EF or stored procedure?

Below is my console application followed by the result. I executed the same application at least 10 times and every time, the time taken by Entity Framework is almost 3-4 times more than the time taken by a stored procedure.

Which of the given code snippet will you use to count related entities without loading them?

Single(blog=> blog.Id = yourCriteriaId). Posts. Count();


3 Answers

No, they are not redundant.

We use Entity Framework for 90% of our data access code.

That 10% is used in the following scenarios:

  • When the code is weighty in logic (overly complicated)
  • When performance is essential
  • When multiple records need to be affected in single transaction

LINQ-Entities (and LINQ, in general) is a brilliant and consistent framework, but there are times that the SQL translated is not as optimal as you expect - extra JOIN's, CASE's, etc. Sometimes in these scenarios it's wise to skip the expression tree translation and go straight to native SQL.

What's more, Entity Framework promotes stored procedures. You can map them on your model, and you can even map procedures directly to CRUD operations on your entities.

So in general, use EF for most simple operations (e.g CRUD), and use stored procedures when performance and complexity are factors.

HTH.

like image 189
RPM1984 Avatar answered Oct 04 '22 11:10

RPM1984


Yes, you should.

Maintaining stored procedures will create huge amounts of friction when you're using an ORM like Entity Framework. EF generates valid, performant SQL, and avoids things like SQL injection attacks.

There may be occasional scenarios where you need to run unusual queries that require complex joins or multiple tables - it's easy in these scenarios to expose a special method on your repository that invokes a stored procedure - but for general-purpose CRUD data access, just let your ORM generate the SQL for you at runtime and stop worrying about it.

(We used to do everything via stored procedures. We switched to NHibernate last year, haven't written a sproc since, and the sky hasn't fallen in or anything...)

like image 41
Dylan Beattie Avatar answered Oct 04 '22 12:10

Dylan Beattie


Just try to move to the EF partially (CRUD operation, etc), but some part of your code leave using SPs. Second case can be applied to the heavy-weight queries, transactions, and when you want to use code defined by you. Generally EF provides development speed, but some specific things conveniently develop using SPs. Also SPs give always give you a performance boost.

like image 26
Ramesh Avatar answered Oct 04 '22 12:10

Ramesh