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?
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:
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.
Return your identity value using an output clause on your insert statement and get it the same way as in 1.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With