Scalar Value from stored procedure via Entity Framework

旧巷老猫 提交于 2021-02-07 05:47:28

问题


I have found a few articles like this one: http://devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values

Yet when I take the step to create a function import for a int32 scalar, this is what gets generated:

 public ObjectResult<Nullable<global::System.Int32>> MyStoredProcedure(Nullable<global::System.Int32> orderId)
    {
        ObjectParameter orderIdParameter;
        if (orderId.HasValue)
        {
            orderIdParameter = new ObjectParameter("OrderId", orderId);
        }
        else
        {
            orderIdParameter = new ObjectParameter("OrderId", typeof(global::System.Int32));
        }

        return base.ExecuteFunction<Nullable<global::System.Int32>>("MyStoredProcedure", orderIdParameter);
    }

I am able to call the procedure with this, but am not able to get to the underlying scalar:

ObjectResult<int?> result = myEntities.MyProcedure(orderId);

In the code examples I have seen, I should get context.MyProcedure().SingleOrDefault().


回答1:


Try this:

int? result = myEntities.MyProcedure(orderId).FirstOrDefault();


来源:https://stackoverflow.com/questions/8230773/scalar-value-from-stored-procedure-via-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!