Somehow I fell from the “happy path” creation of a stored procedure and the Entity Framework.
I had to make changes to a stored procedure and it got deleted from the Entity Data Model Xml file (*.edmx) resulting in the following error message when trying to compile the project:
System.Data.EntityCommandCompilationException
An error occurred while preparing the command definition. See the inner exception for details.
Inner exception:
{“The specified FunctionImport is not mapped to a store function and cannot be executed.”}
There is an easy solution to fix that error.
How to Solve FunctionImport Error Message
First open your edmx file and right click on the model that owns the stored procedure.
Click Add then select “Add Function Import“.
Add the same Function Import name that is used in your Context file (if like me, the method was already created but messed up, otherwise is all new and it will be recreared anyways).
Select the Stored Procedure Name that you are trying to fix.
Choose the Entities and click OK.
A new window might pop up “Verify that the FunctionImport name is unique”.
If that is the case, and you get the “Verify that the FunctionImport name is unique” window popup, do the following:
Open your *.edmx file and right click over the model you want to update.
Select “Show in Model Browser”.
Now the Model Browser window opens up.
Go to: {myProject}.DataModel > EntityContainer: {somethingEntities} > Function Imports.
The function import causing the problem should be there, just delete it and save the *.edmx file.
Try to add the Function Import again.
Voila! no issues this time.
Save the *.edmx file and recreate the context file (by making a simple non-invasive change like adding a space to the {myProject}.Context.tt file). make sure the new method:
public virtual ObjectResult<MyEntity> <MyEntity>_NameoftheSP(parametets) is present in your Context file.
After that I get no more errors at build time or runtime.
Peter K.
Thanks! That saved me some time. 🙂
DaCoder
No problem, glad to help. Thanks for your comment.
David
Thanks man, really useful 😀
John H
I see this happening every time I make a change to the data model. Is there a setting that torches all my stored procedure mappings every time I make a change? It’s been pretty consistent and it makes me wonder if EF is worth the effort. It’s been a huge time savings, but if I have to manually remap every single stored procedure every time I make a change to the data model, it’s going to outlive its usefulness quickly.
Kamil
Thanks for the info. Stupid visual studio…
Rod
Thank you for burning the brain cells on this issue!
Robert J. Good
Thanks for the help!
I did have a situation, where I would 1. Delete the entity, 2. Re-add it, 3. The error would still show up.
What I had to do was:
1. Right click the .edmx file in Solution Explorer.
2. Click OpenWith-TextEditor
3. Search on the function in the error message: Insert
4. Manually delete all lines between
This happened when I renamed an underlying table view, and the .edmx had ‘abandoned’ old functions.
Kim P
I had this same/similar issue where you could see both the stored procedures and function imports but they were still disconnected. I happened to trip across the solution once I looked at the properties windows for each function import and noticed the stored procedure name property was missing for each. Once I populated these fields all was working well again.
Regards,
Kim
Cuong Tran
thanks!
thiru reddy
Thanks a lot it help to me.