映射存储过程不会返回一个字符串

时间:2014-02-20 16:45:24

标签: entity-framework

我正在尝试在Entity Framework中映射以下存储过程。它显然应该返回一个字符串,但我不能这样做。当我执行函数导入时,它映射到选择返回标量字符串的集合。如果我将它设置为None,它只返回一个int。是SP吗?是否不支持选择存储过程?

ALTER PROCEDURE [dbo].[getCurrentConnection]
    @registrationID VARCHAR(100)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT TOP 1 ConnectionGUID FROM clientconnection WHERE registrationid = @registrationID ORDER BY created desc
END

生成的代码执行:

 public virtual ObjectResult<string> SPgetCurrentConnection(string registrationID)
        {
            var registrationIDParameter = registrationID != null ?
                new ObjectParameter("registrationID", registrationID) :
                new ObjectParameter("registrationID", typeof(string));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("SPgetCurrentConnection", registrationIDParameter);
        }

我也无法从我的模型中删除SP并重新开始。我没有在设计师身上看到它。它只允许我修改并在其前面提供前缀SP

1 个答案:

答案 0 :(得分:0)

通过修改SP以返回输出参数来解决。

ALTER PROCEDURE [dbo].[getCurrentConnection]
    @registrationID VARCHAR(100),   
    @connectionID varchar(100) OUTPUT

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT @connectionID = (SELECT TOP 1 ConnectionGUID FROM clientconnection WHERE registrationid = @registrationID ORDER BY created DESC)
END

然后在代码中编写函数:

public string GetCurrentConnection(string accountId)
        {

            using (savitasEntities2 db = new savitasEntities2())
            {
                string cid = String.Empty;
                ObjectParameter connectionId = new ObjectParameter("connectionID", typeof(string));
                db.GetCurrentConnection(accountId, connectionId);
                return connectionId.Value.ToString();
            }
        }