9

So I've got a DB in SQL Server that I'm connecting to and using Entity Framework 4.1 to generate my POCO classes, which works generally pretty well. There are also stored procedures that I am using the 'function import' feature to create retrieve the resulting rows of data from calling them. Essentially the process I'm using is to:

  1. Right-click on the Model.edmx and choose "Function Import..."
  2. Pick the procedure from the dropdown
  3. Enter my desired Function Import Name
  4. Click "Get Column Information"
  5. Click "Create New Complex Type"
  6. Click "OK"

and that will create a POCO class for the result set definition and I can do something like:

var query = context.GetMyStuff().AsQueryable();

to retrieve the results. This seems to work just fine.

Now the trouble I'm having is when I try to modify a stored procedure and then get the changes to propagate to my code. For instance, I added an additional column to a table and then updated the stored procedure to return that column data as part of the results. I don't see how to make that update propagate into the function import stuff, i.e., get the generated POCO to have a new property for that added column.

What's the drill to make that update to the procedure reflect back in C# side? Am I going to have to make a new class each time? Wasn't obvious to me how to do this.


Additional Info:

When I've tried to "Update" the Complex type, as suggested in the response by Ladislav to this question, I get an error message "Verify that the FunctionImport name is unique."

If I try what E.J. Brennan suggests below, I get the same error message.

What does work, at least for me, is to open the Model.edmx file in Notepad++, find the FunctionImport line and delete it then regenerate it. That's not ideal, but it worked.

Community
  • 1
  • 1
itsmatt
  • 31,265
  • 10
  • 100
  • 164
  • I'm not sure if this is the same question: http://stackoverflow.com/questions/6381423/function-import-when-sp-is-changed – Ladislav Mrnka Jul 14 '11 at 12:47
  • @Ladislav - saw that, but when I try to do that I get a error. I'll edit my question above to reflect that. – itsmatt Jul 14 '11 at 12:53

5 Answers5

26

(This solution is for EF 6. I have not tried in other EF versions. It works nice.) I am supporting Brennan's answer by illustrating an example to make it more clear to naive coders like me :)


Go to Model browser. MyStoreProc is the name of the stored procedure (as an example). MyStoreProc will appear in 3 places.

  1. 1st place- Under Complex Types-> as MyStoreProc_result
  2. 2nd Place- Under Function Imports -> as MyStoreProc
  3. 3rd Place - Under Stored Procdures/ Functions -> as MyStoreProc enter image description here

Delete all three from model. Save the edmx (by clicking in the window then ctrl+S). Then right click and click update the model from database. Then add the updated stored procedure and Save again.

Solved without any hassle :)

SArifin
  • 2,746
  • 1
  • 15
  • 15
9

I usually go into the model browser, In main menu select View > Other Windows > Entity Data Model Browser (it usually opens in the same panel as Solution explorer), and delete the stored procedure that has changed, and re-add it back in.

There may be an easier, but this one works for me.

Edit: "Update model from database" theoretically should also work, but in my experience, it doesn't work 100% of the time, deleting and re-adding seems rock solid.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • Thanks for the response - tried this but had same problem as I put in my additional note at the end of my question. If I edit the .edmx file directly (the xml, that is) and delete the one I'm trying to update (the FunctionImport line), it works but this seems a bit clunky. – itsmatt Jul 14 '11 at 13:06
  • When you were in the model browser, did you delete the stored procedure AND delete the function import - two seperate deletes? That works for me without errors - just confirmed it. – E.J. Brennan Jul 14 '11 at 13:43
  • I deleted the function import, then deleted the stored proc, then did an update from the db which brought back in the deleted stored proc. Did a right-click on the model.edmx, Function Import, re-entered my desired Function Import Name, chose the Stored proc from the dropdown, clicked get columns button, clicked 'Create New Complex Type', which gave me a complex type name of MyFuncImportName_Result, clicked OK and got the "Verify that the FunctionImport name is unique." dialog. Appears that the model.edmx still has an entry in it - if I delete it manually from the xml, it will then work. – itsmatt Jul 14 '11 at 14:58
2

At our company we used to update the SP's like SArifin answer...which works but is kind of a drag.

Recently we found out that you only have to go to that SP Function Import -> Right Click -> Edit -> Update the complex type.

Bernardo
  • 531
  • 1
  • 13
  • 31
  • Thanks for this, I used to delete the 3 seperate instances of each SP (inc complex type) but your suggestion works and is much quicker... – NickGPS Nov 08 '16 at 17:15
1

@itsmatt - your solution worked for me. In short,

  1. Open file .edmx in a text editor.
  2. Locate and delete the XML node that looks like:
<FunctionImport Name="<some name>" ReturnType="Collection(dev_riskModel.GetECLExcesses_Result)">
(additional lines. . .)
</FunctionImport>
  1. Save the file
  2. In the model browser, update the model from the database, re-adding the stored proc
  3. Select the newly added stored proc, then select the "Add Function Import" item from the context menu, etc.
1

I just faced this same issue and thank you for your answer! One more thing I had to do was to delete the output from the "Complex Types" in the model browser.

Tojamismis
  • 59
  • 3
  • That should have been a comment, not an answer. I know you need some more rep to comment. – bummi Sep 05 '13 at 06:46