visual-studio 具有非实体返回类型的实体模型中的函数导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/578536/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function Imports in Entity Model with a non-Entity Return Type
提问by Robert Claypool
I have a stored procedure in my Entity Data Model and added it to the function imports.
我的实体数据模型中有一个存储过程并将其添加到函数导入中。
Problem is... Visual Studio generates the function code in the model's code-behind if and only if I specify the return to be an entity type. Scalar and null return types do not work. Visual Studio does not generate the function code when I choose them.
问题是......当且仅当我将返回指定为实体类型时,Visual Studio 才会在模型的代码隐藏中生成函数代码。 标量和空返回类型不起作用。当我选择它们时,Visual Studio 不会生成函数代码。
Is there something I am missing, or is this a bug?
Any work-arounds? 
有什么我遗漏的吗,或者这是一个错误?
任何解决方法?
使用 Visual Studio 2008 v9.0.30729.1 SP (Service Pack 1)
采纳答案by Craig Stuntz
It's not so much a bug as it is the lack of a feature. The Entity Framework just doesn't support stored procedures returning scalar values right now. I believe this is supposed to change in .NET 4.0. In the meantime, you can execute such a stored procedure by using the store connection, available via CreateDbCommand.
与其说是错误,不如说是缺乏功能。实体框架现在不支持返回标量值的存储过程。我相信这应该在 .NET 4.0 中改变。同时,您可以使用存储连接执行这样的存储过程,可通过CreateDbCommand 获得。
回答by Craig Stuntz
Since the only thing that works now is to map the return type to an entity, one workaround is to create a view that corresponds to the return data and create an entity for the view. This will only work if the SP is doing a SELECT to return a result set, not a return value. I got this to work with a sample app, like so: SP:
由于现在唯一有效的是将返回类型映射到实体,因此一种解决方法是创建一个与返回数据对应的视图并为该视图创建一个实体。这仅在 SP 执行 SELECT 以返回结果集而不是返回值时才有效。我让它与示例应用程序一起使用,如下所示:SP:
ALTER PROCEDURE [dbo].[DoSomething]
    @param1 varchar(50),
    @param2 varchar(50)
AS
BEGIN
    DECLARE @ID INT
    SET NOCOUNT ON;
    INSERT tmp_header (fname, lname) VALUES (@param1, @param2) 
    SET @ID = SCOPE_IDENTITY()
    SELECT @ID AS 'id'
END
VIEW:
看法:
CREATE VIEW [dbo].[View_1]
AS
SELECT   0 as  id
Create the function import setting the return type to View_1 and build the model.
创建函数导入,将返回类型设置为 View_1 并构建模型。
in code:
在代码中:
    class Program
    {
      static void Main(string[] args)
      {
        using (PS_RADSTESTEntities ctx = new PS_RADSTESTEntities())
        {
          EntityConnection ec = ctx.Connection as EntityConnection;
          ec.Open();
          DbTransaction txn = ec.BeginTransaction();
          ObjectResult<View_1> result = ctx.DoSomething("Toby", "Kraft");
          View_1 row = result.Single();
          int id = row.id;
// do some other interesting things ...
          ctx.SaveChanges();
          txn.Commit();
        }
      }
    }
Be sure to set the column names exactly the same between the view and SP. Toby
确保在视图和 SP 之间设置的列名称完全相同。托比
回答by Darius Kucinskas
I had the similar problem and solved it using this article:
我遇到了类似的问题并使用这篇文章解决了它:

