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

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

问题描述

我想从实体框架的存储过程返回一个int列表。

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.

它自动生成一个复杂类型只返回一个int,而不是设置一个结果。

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?

推荐答案

我创造了这个示例存储过程返回列表INT值:

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

然后我加了此存储过程在我的EF 的.edmx 模型,并创造了这个功能导入:

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

查询存储过程让我发现,它返回一个结果集,包括 INT 值 - 因此,我定义的返回值是的标量的集合:的Int32 的在功能导入对话框

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天全站免登陆