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:
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.
In the EDM designer, right click on the Student entity and select Stored Procedure Mapping to open Mapping details, as shown below. In the Mapping Details, as shown below, you will see <Select Insert Function>, <Select Update Function>, and <Select Delete Function>.
Right-click a complex type in the Model Browser, point to Add, then point to Scalar Property or Complex Property, then select the desired property type. Alternatively, you can select a complex type and then press the Insert key on your keyboard.
A complex type is a set of properties that exist in its own object for C#, but are mapped to columns on an already existing table (the one for the entity that contains it), instead of having its own table (which would need a key, etc.).
You can use stored procedures either to get the data or to add/update/delete the records for one or multiple database tables. EF API creates a function instead of an entity in EDM for each stored procedure and User-Defined Function (UDF) in the target database.
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.
(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.
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 :)
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