Why can't Entity Framework see my Stored Procedure's column information?

后端 未结 10 1288
终归单人心
终归单人心 2020-12-02 14:54

I have the following stored procedure and when I attempt to Function Import it says my Stored Procedure returns no columns. What am I missing? Any Suggestions?

The

相关标签:
10条回答
  • 2020-12-02 14:55

    Whats happening here behind the scenes?

    1. While doing function import -> Get Column Information ... Visual Studio executes the stored proc with all the param values as NULL (you can cross-check this through MS SQL Profiler).

    2. Doing step 1, the stored proc's resulting columns are returned with its data type and length info.

    3. Once the column info is fetched, clicking on 'Create New Complex Type' button creates the Complex type of the SP in contention.

    In your case, the stored proc params are not nullable, hence the Visual Studio call fails and returns no columns.

    How to handle this?

    IF (1=0) 
    BEGIN 
        SET FMTONLY OFF 
        if @param1 is null and @param2 is null then
            begin
                select
                cast(null as varchar(10)) as Column1,
                cast(null as bit) as Column2,
                cast(null as decimal) as Column3
            END
    END   
    

    To be precise (in your case):

    IF (1=0) 
    BEGIN 
        SET FMTONLY OFF 
        if @SearchString is null then
            BEGIN
                select
                cast(null as int) as ID,
                cast(null as varchar(255)) as VendorName,
                cast(null as varchar(255)) as ItemName,
                cast(null as varchar(2)) as Type
            END
    END   
    

    Reference: http://mysoftwarenotes.wordpress.com/2011/11/04/entity-framework-4-%E2%80%93-the-selected-stored-procedure-returns-no-columns-part-2/

    0 讨论(0)
  • 2020-12-02 14:58

    You're having this problem due to the temp table.

    All you need to do is:

    1. Alter your stored procedure to return the select statement without the temp table.
    2. Go to the function import and get the column information.
    3. Alter your stored procedure back to the original.
    0 讨论(0)
  • 2020-12-02 14:59

    If you are using temporary table, the Entity (EDMX) cant understand what is going on.

    So return empty result with the columns name, comment out all your stored procedure and execute in the sql manager, then get the complex type in visual studio. After saving, return your stored procedure to it's original state (uncommented that is).

    good luck/

    0 讨论(0)
  • 2020-12-02 14:59

    This is the only correct answer and can be found from here

    https://stackoverflow.com/a/27960583/511273

    Basically, EF knows that it's always going to return the number of rows or -1 if NO COUNT is on, or anything returned from the SQL stored procedure that's called by return <some integer>. For that reason, no matter what stored procedure you import, the type will always be nullable<int>. You can only return an integer from an SQL stored procedure. So, EF gives you a way to edit your function. I would imagine that if you edited it manually you would overwrite it on refresh, but I can't confirm that. Either way, this is the facility provided by EF to deal with this issue.

    Click on your .emdx file. It has to be the one you selected in the Solution Explorer. Select Model Browser (Right beside Solution Explorer tab, above Properties). Expand Function Imports, locate your stored procedure, right click, click Edit. Select your variable type. It can either be a primitive type or you can click Get Complex Type. Click Get Column Information. I have confirmed this survives a model refresh.

    Why can you only return an integer from a stored procedure? I don't really know, but this return definition explains that you can only return an integer: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=sql-server-ver15

    0 讨论(0)
  • 2020-12-02 15:03

    Try adding this line to the beginning of your stored procedure:

    SET FMTONLY OFF
    

    You can remove this after you have finished importing.

    0 讨论(0)
  • 2020-12-02 15:03

    As a quick and dirty way to make EF find the columns, comment out the where clause in your stored proc (maybe add a TOP 1 to stop it returning everything), add the proc to the EF and create the Complex Type, then uncomment the where clause again.

    0 讨论(0)
提交回复
热议问题