I have a WebAPI service with a SQL Server backend which is managed using Entity Framework (code-first). I have used Linq queries to retrieve data from the database. Since a few of my API endpoints do multiple joins to get data, I observed some delay when deployed in production. I read on using ORM mapping vs direct SQL queries (stored procedures) and came to a conclusion that for complex queries, it's optimal to use stored procedures.
I came across few articles explaining how to return value from stored procedure using code like:
context.Database.SqlQuery<T>(...)
However, all the examples deals with simple SELECT
query and yes, I also came across returning multiple SELECT
query results too. But in that case, it's 2 SELECT
queries.
In real scenario, it can be more than 2 select queries since it may involves joins and other codes. And, at the end of the query only we'll be deducing the final SELECT
query which returns the expected data. Below is a sample mock-up code to explain the scenario:
// Inside my stored procedure
Declare @XId uniqueidentifier
Declare @YId uniqueidentifier
// Some mockup queries. This can have JOINS and other complex codes in real code.
// Below code is made as simple as possible to explain the scenario
Select @XId=Id From Table1 Where Name = 'Name1'
Select @YId=Id From Table2 Where Code = 'Code1'
// This is the data, I am interested
Select * From Table3 Where Col1Id=@XId And Col2Id=@YId
How can I get rows from last SELECT
query achieve this?
I did some research and assume that it's not possible to do with EF since stored procedure support is minimal. Is that possible to do within EF limits or do I need to fall back to ADO.NET for this?
Any help will be appreciated.
Thanks.