Use stored procedure in Entity Framework (code first)

I use this code to define my stored procedure

CREATE PROCEDURE [dbo].[SP]
(@Country NVARCHAR(20))
AS
BEGIN
    SET NOCOUNT ON;
    SELECT c.*,O.* from Customers
           as c inner join orders O on c.CustomerID=o.CustomerID

     where  c.Country=@Country 
END

and this is my C # code:

IList<Entities.Customer> Customers;

using (var context = new NorthwindContext())
{
   SqlParameter categoryParam = new SqlParameter("@Country", "London");
   Customers = context.Database.SqlQuery<Entities.Customer>("SP @Country",  categoryParam).ToList();
}

The problem is here:

I want to report data from a table Orders, and my stored procedure generates this for me. How can I get the data Ordersin my c # code? Remember that I want to execute this stored procedure only once.

+5
source share
1 answer

Entity Framework Code ? http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-4-1-using-stored-procedure-to-insert-data.aspx, DbContext.

, , , ? ? , , . , (.. ), (eww).

, linq :

from c in context.Customers.Include(c=>c.Orders)
where c.Country == country
select c;

, EF , , -, .

+7

All Articles