从EntityFramework调用存储过程时出错 [英] Error calling Stored Procedures from EntityFramework

查看:1116
本文介绍了从EntityFramework调用存储过程时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已按照以下步骤操作:



我正在尝试从EntityFramework访问商店程序。首先我在Azure数据库中创建了存储过程:





然后,我从数据库更新了.edmx模型,只选择了我想要的StoredProcedure。





一旦完成,在功能导入我看到StoredProcedure添加,但不是在StoredProcedures部分。
我可以做什么,以便它出现在这里?





在功能导入部分中,所有参数均设置为输入,MaxReference应标记为输出。
如何更改?





尽管这两个问题我已经执行了代码:





我得到以下例外:

  EntityCommandCompilationException 
准备命令定义时发生错误。查看内部例外情况。

和InnerException:

 函数import'DataModelEntities.AssignMaxSalesRef'无法执行,因为它没有分配给存储功能。 

解决方案

你可能想参考这篇博文: FunctionImport未映射到存储函数错误,它讨论一个类似的问题。原因显然是:


我必须对存储过程进行更改,并从实体数据模型Xml文件(*)中删除。 edmx)


通过以下分步解决方案:


有一个简单的解决方案来解决这个错误。首先打开您的edmx文件,然后右键单击拥有存储过程的模型。单击添加,然后选择添加功能导入。



添加与上下文文件中使用的相同的功能导入名称(如果像我一样,该方法已经创建但已经混乱,否则是全新的,它将被重新创建)。选择您要修复的存储过程名称。选择实体,然后单击确定。可能会弹出一个新窗口验证FunctionImport名称是否唯一。



如果是这种情况,并获得验证FunctionImport名称是否唯一窗口弹出窗口,请执行以下操作:打开* .edmx文件,然后右键单击要更新的模型。选择在模型浏览器中显示。现在,模型浏览器窗口打开。转到:{myProject} .DataModel> EntityContainer:{somethingEntities}>函数导入。导致问题的函数导入应该在那里,只需删除它并保存* .edmx文件。



尝试再次添加函数导入。瞧!这次没有问题。保存* .edmx文件并重新创建上下文文件(通过进行简单的非侵入式更改,如向{myProject} .Context.tt文件添加一个空格)。确保新的方法:



public virtual ObjectResult< MyEntity>


另一种类似的疑难解答资源关于更新edmx文件的分步说明(和图像!):无法执行功能导入,因为它未映射到商店功能


I am trying to access a Store Procedure from EntityFramework.

I have followed these steps:

First of all I have created the Stored Procedure in the Azure Database:

Then, I have updated the .edmx model from database, selecting only the StoredProcedure I want.

Once done, in the Function Import I see the StoredProcedure added, but not in the section of StoredProcedures. What can I do so that it appears here?

In the Function Import section, all the parameters are set as Input, whereas "MaxReference" should be marked as Output. How can I change it?

Although these two issues I have executed the code:

and I got the following exception:

EntityCommandCompilationException
An error occurred while preparing command definition. See the inner exception for details.

and the InnerException:

The function import 'DataModelEntities.AssignMaxSalesRef' cannot be executed because it is not assigned to a storage function.

解决方案

You may want to refer to this blog post: FunctionImport is not mapped to a store function Error, that discusses a similar problem. The cause apparently being:

I had to make changes to a stored procedure and it got deleted from the Entity Data Model Xml file (*.edmx)

With the following step-by-step solution:

There is an easy solution to fix that error. 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.

Another troubleshooting resource with similar step-by-step instructions (and images!) on updating the edmx file: The function import cannot be executed because it is not mapped to a store function.

这篇关于从EntityFramework调用存储过程时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆