Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 Function Import not working

I am using Entity Framework 4 with the POCO code generator. I have a stored procedure that does an INSERT and returns the @@IDENTITY of the inserted record. I am trying to import the stored procedure as a function in my .edmx file, but I am having trouble using it.

In the model browser, I can see the stored procedure under the database heirarchy, and then I right-click and select "Function Import..." I have tried using "None" as the return type as well as Int32 (even though it says "Collection of.."). The function appears under Function Imports, but even after saving and compiling, I am unable to find the function anywhere in my ObjectContext. I have attempted to delete it and re-import the stored procedure several times with no success.

NOTE: I have another stored procedure that does a straight SELECT and this is imported properly and shows up in the ObjectContext code.

Am I doing something wrong?

like image 875
dotariel Avatar asked Sep 29 '10 20:09

dotariel


1 Answers

If your stored procedure does not return a result set, so you select "Returns a Collection of" "None" in the "Add Function Import" dialog in Visual Studio, then the function import is NOT added as a method on your generated object context. (I have not been able to find out why yet, but I'm still looking.)

The return value from the sproc (e.g., return @@identity) is not what is meant by the "Returns a Collection of" question. Which is why it didn't work. The question is asking about what result set comes back from the sproc.

There are three ways I can think of to handle your problem:

  1. Return your identity value using a select (e.g., select @@identity as Identity) and then specify a collection of Int32 in reply to the "Returns a collection of" question.

  2. Return your identity value using an output clause on your insert statement and get it the same way as in 1.

  3. Use Entity SQL and make the identity value an out parameter. Here is how to do that: How to: Execute a Query Using a Stored Procedure with In and Out Parameters

I hope that helps.

like image 54
grahamesd Avatar answered Nov 15 '22 07:11

grahamesd