存储过程中的实体框架返回列表 [英] Entity Framework Return List from Stored Procedure

查看:93
本文介绍了存储过程中的实体框架返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Entity Framework中的存储过程返回int的列表。



我创建了存储过程,并将其添加到Entity Framework中。我试图将它绑定到一个复杂的类型,但当我打开函数导入。



它会自动生成一个只返回一个int而不是一个结果集的复杂类型。



有没有人知道我如何导入一个返回列表作为结果集的实体?

解决方案

我创建了这个示例存储过程,返回一个int值:

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

然后,我将这个存储过程添加到我的EF .edmx 模型中,并创建了此功能导入: / p>



查询存储过程显示我返回一个由 int 值组成的结果集 - 因此我将返回值定义为函数导入对话框中的Scalar:Int32 的集合。



之后,我可以调用该存储过程并得到如下结果:

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

foreach(int intValue in result.AsEnumerable())
{
Console.WriteLine(INT value returned:{0},intValue);
}
}


I'm trying to return a list of int from a stored procedure in Entity Framework.

I created the stored procedure fine, and added it into Entity Framework. I'm trying to bind it to a complex type of but when I open the function import.

It auto generates a complex type that only returns an int instead of a results set.

Does anyone know how I can import an entity that returns a list as a result set?

解决方案

I created this sample stored procedure returning a list of int values:

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

I then added this stored procedure to my EF .edmx model and created this Function Import:

Querying the stored procedure shows me that it returns a result set consisting of int values - I therefore define the return value to be a collection of Scalar: Int32 in the function import dialog.

After that, I can call that stored procedure and get back the results like this:

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

    foreach (int intValue in result.AsEnumerable())
    {
        Console.WriteLine("INT value returned: {0}", intValue);
    }
}

这篇关于存储过程中的实体框架返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆