开发者

How to add a stored procedure that returns a scalar value to Entity Framework model and call it from the Entity Context?

I have a stored procedure开发者_运维问答 that returns a scalar value, I mapped it in the entity model, created an imported function, however it wasn't add into the context class. So I was not able to found how to call it from C# code.

Thanks in advance, Carlos Loth.


In EF 1, it's a little involved.

In EF 4, this is directly supported.


This is 2013, with EF Beta 6 AND STILL, we cannot achieve this. Different to what ^ Craig ^ Responded, you can get a "COLLECTION" of thingies rather than the thingy itself. They DID change this in EF 5 (WARNING: snarky comment comming), they changed the UI to say "Returns a Collection Of".

As UGLY as it may seem, I sometimes create EF_SP_foo wrappers that all they do is a "SELECT" for the scalar value and return it with a Standarized name "Result"

CREATE EF_SP_foo
    @ID bigint
AS
  BEGIN
      DECLARE @newID bigint
EXEC @newID = SP_foo @ID

      SELECT @newID AS Result  -- I really hate this part
  END
  • UPDATE * Although, if you DON'T specify Return type, it will be treated as an INT function (used mainly for rowcounts or State IDs to return I suppose)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜