Entity Framework Return List from Stored Procedure

c# entity-framework stored-procedures

Question

I'm attempting to have an Entity Framework stored procedure return a list of int values.

The stored procedure was successfully generated and added to Entity Framework. I'm attempting to tie it to a complex type, but the function import doesn't work when I open it.

Instead of returning a results set, it automatically builds a complicated type that only returns an int.

Can somebody tell me how to import a result set from an entity that produces a list?

1
14
3/12/2015 2:50:22 PM

Accepted Answer

This example stored procedure I made returns a list of integer values:

CREATE PROCEDURE dbo.GetListOfInt
AS BEGIN
    SELECT *
    FROM 
    (VALUES (42), (4711), (8088), (80286), (80486), (655235)) AS VT(VC)
END

This stored method was then added to my EF..edmx generated this model and function import:

enter image description here

The stored method provides a result set that includes when I query it, according to what I can see.int values - I consequently specify a grouping of Int32 Scalar as the return value in the function import dialog.

I can then use the saved method to retrieve the results in the following manner:

using (testEntities ctx = new testEntities())
{
    ObjectResult<int?> result = ctx.GetListOfInt();

    foreach (int intValue in result.AsEnumerable())
    {
        Console.WriteLine("INT value returned: {0}", intValue);
    }
}
15
3/12/2015 3:12:47 PM

Popular Answer

You don't need to map anything specific if all you're attempting to do is retrieve a list from a stored process.

Just say it out loud:

var results = dbContext.Database.SqlQuery<int>("SP_YourSP").ToList();

this ought to produce a list of integers.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow