Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a stored procedure in entity framework 3.5

I am using .NET 3.5 SP1, VS 2008 with the entity framework. I originally added a stored procedure to my model which took 2 parameters. Later, I changed the sproc to just take 1 parameter. I ran the Update Model From Database menu option and confirmed that my sproc was listed in the Refresh tab. Finished up the wizard, cleaned and rebuilt the solution and the model never changed the signature of the sproc.

Why isnt it updating? Is the Update model from database feature broken? Am I missing something? Thanks.

like image 406
Jeff Avatar asked Feb 14 '11 17:02

Jeff


People also ask

Which is faster EF or stored procedure?

Below is my console application followed by the result. I executed the same application at least 10 times and every time, the time taken by Entity Framework is almost 3-4 times more than the time taken by a stored procedure.

Is stored procedure faster than EF core?

I know with stored procedures we get stored execution plans so stored procedure's execute faster next time, as the plan doesn't need to get regenerated again. Therefore, for an enterprise system, EF Core would quite a bit slower for data operations than using, say, something like Dapper and Stored Procedures.


2 Answers

I've seen two problems with updating a Stored Procedure after adding it to the entity framework:

  • The Function Import created to call the SPROC wasn't updated (incorrect, out-of-date parameters)
  • The Complex Type created for the SPROC's result set wasn't updated (incorrect, out-of-date fields)

Updating EDMX model (function import)

  1. Open the .edmx file (in the GUI designer).
  2. Right-click in some open space and select "Update Model from Database".
  3. Click Finish (when able) on the pop-up window. Your SPROC's signature should be updated (along with any Function Imports).

Updating stored procedure complex type

  1. open the "Model Browser" window while you have the .edmx open.

  2. Expand the ContentModel, EntityContainer: ..., and then Function Imports nodes. Look for the function import for your stored procedure and double click it. The same window you used to create it will open, but now populated with its data.

  3. Click the Get Column Information button (look at the grid below the button to see what will be changed).

  4. Click the Update button next to the "Complex" radio button choice.

  5. Click OK, and the Complex Type for your result set should be updated.

like image 198
DaveD Avatar answered Sep 21 '22 12:09

DaveD


You don't have to edit the edmx / xml. DO THIS:

When you bring a stored procedure into the EDMX (I'm talking about one that returns a result set, but it's going to be a similar process), 3, not 1, items are created, and that is the source of the problem. (*** the second source of the problem is that you have to explicitly SAVE after deleting objects, before re-importing objects, otherwise the newly re-imported objects will clash with what's not been truly deleted (by saving) from the edmx.

SO! Do this:

In Model Browser, usually on the left, paste the name of your stored procedure into the search box, click enter (or search). It will land on the first instance - probably the stored procedure itself. Right click and delete. Now, put cursor back in search box -> hit enter or search again. Now you will land on the function import. Right click and delete. Do the same thing a third time - this will land on the complex type representing the result set. Delete it. Now you're done NOT!!!!!. You have to save what you just did, so that the deletions are written (un-written) in the edmx.

Now, go get the stored procedure again (updated model / select the stored proc). NOW CLICK SAVE AGAIN.

That will work every time. The key is make sure when using the model browser to search ALL instances of the stored procedure name appearing in the model browser, and because of that, it's better not to fuss around with the default naming conventions. Second key is to SAVE after you complete any step.

like image 42
jamescnectnet Avatar answered Sep 18 '22 12:09

jamescnectnet