Calling scalar function from c# using Entity Framework 4.0 / .edmx

前端 未结 5 1373
迷失自我
迷失自我 2020-12-28 15:39

I would like to map my scalar function to my .edmx but it fails. I right click on my entity framework mapping, and choose update model from database. It appears in my stored

5条回答
  •  灰色年华
    2020-12-28 16:08

    The one and the only solution is to convert the function scalar type to table value type with a single value in the table, please see the code sample.

    You don't have to change anything in the EDMX XML, please modify the SQL function

    Scalar function as it was, which doesn't work

    CREATE FUNCTION [dbo].[GetSha256]
    (
        -- Add the parameters for the function here
        @str nvarchar(max)
    )
    RETURNS VARBINARY(32)
    AS
    BEGIN
        RETURN ( SELECT * FROM HASHBYTES('SHA2_256', @str) AS HASH256 );
    END -- this doesn't work.
    

    Scalar function -> Converted to Table Valued function , it works

    CREATE FUNCTION [dbo].[GetSha2561]
    (
        -- Add the parameters for the function here
        @str nvarchar(max)
    )
    RETURNS  @returnList TABLE (CODE varbinary(32))
    AS
    BEGIN
    
        INSERT INTO @returnList
        SELECT HASHBYTES('SHA2_256', @str);
    
        RETURN; -- This one works like a charm.
    
    END
    

    Edmx screenshot

提交回复
热议问题