locked
How to read single value return by store proc with EF code first RRS feed

  • Question

  • suppose i am calling store proc by EF code first and store proc return value like this way

    select 'SUCCESS'

    or

    select 1

    OR

    Return 1

    just tell me how to read the single value return by store proc with EF code first. looking for a small sample code.

    Thursday, September 15, 2016 3:12 PM

Answers

  • Hi Mou_inn,

    Based on your description, I create a simple demo by using dbcontext.Database.SqlQuery  as below for your reference.

    # Store Procedure

    CREATE PROCEDURE [dbo].[Sp_SingleValue]
    AS
    BEGIN
    
    --DO STUFF
    
    Select 1
    END

    #Usage:

    using (var db = new EFDemoContext())
                {
                   
                    var result = db.Database.SqlQuery<int>("Sp_SingleValue").FirstOrDefault();
                
                }

    I hope it is helpful to you.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Friday, September 16, 2016 1:21 PM
    Friday, September 16, 2016 1:34 AM
  • we can do it like this way too. code taken from http://www.mikesdotnetting.com/article/299/entity-framework-code-first-and-stored-procedures

    CREATE procedure [dbo].[CountOfOrders]
        @ProductId int
    AS
    SELECT Count(*) From Orders o 
    INNER JOIN OrderDetails od ON od.OrderID = o.OrderID 
    WHERE od.ProductID = @ProductId
    SqlParameter param1 = new SqlParameter("@ProductID", 72); 
    var totalOrders = await context.Database.SqlQuery<int>("CountOfOrders @ProductID", param1).SingleAsync();



    • Marked as answer by Sudip_inn Friday, September 16, 2016 1:22 PM
    Friday, September 16, 2016 1:21 PM

All replies

  • Hi Mou_inn,

    Based on your description, I create a simple demo by using dbcontext.Database.SqlQuery  as below for your reference.

    # Store Procedure

    CREATE PROCEDURE [dbo].[Sp_SingleValue]
    AS
    BEGIN
    
    --DO STUFF
    
    Select 1
    END

    #Usage:

    using (var db = new EFDemoContext())
                {
                   
                    var result = db.Database.SqlQuery<int>("Sp_SingleValue").FirstOrDefault();
                
                }

    I hope it is helpful to you.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Friday, September 16, 2016 1:21 PM
    Friday, September 16, 2016 1:34 AM
  • we can do it like this way too. code taken from http://www.mikesdotnetting.com/article/299/entity-framework-code-first-and-stored-procedures

    CREATE procedure [dbo].[CountOfOrders]
        @ProductId int
    AS
    SELECT Count(*) From Orders o 
    INNER JOIN OrderDetails od ON od.OrderID = o.OrderID 
    WHERE od.ProductID = @ProductId
    SqlParameter param1 = new SqlParameter("@ProductID", 72); 
    var totalOrders = await context.Database.SqlQuery<int>("CountOfOrders @ProductID", param1).SingleAsync();



    • Marked as answer by Sudip_inn Friday, September 16, 2016 1:22 PM
    Friday, September 16, 2016 1:21 PM