从 EntityFramework 调用存储过程时出错

Error calling Stored Procedures from EntityFramework

我正在尝试从 EntityFramework 访问存储过程。

我已按照以下步骤操作:

首先,我在 Azure 数据库中创建了存储过程:

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

完成后,在函数导入中我看到添加了 StoredProcedure,但没有在 StoredProcedures 部分中。 我该怎么做才能让它出现在这里?

在Function Import部分,所有参数设置为Input,而"MaxReference"应标记为Output。 我该如何更改它?

虽然这两个问题我都执行过代码:

我遇到了以下异常:

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

和 InnerException:

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

您可能需要参考此博客 post: FunctionImport is not mapped to a store function Error,其中讨论了类似的问题。原因显然是:

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

使用以下分步解决方案:

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.

另一个关于更新 edmx 文件的具有类似分步说明(和图像!)的故障排除资源:The function import cannot be executed because it is not mapped to a store function

这 post 应该是一条评论,但我没有足够的代表发表评论。

我遇到了类似的问题。我的存储过程是可见的,但我仍然收到错误。 Alex 的这个问题和答案让我查看了模型浏览器中的函数导入,我看到每个存储过程都有多个条目。他们有序列号以防止他们成为真正的重复者。我删除了 Function Imports 下的所有内容以及 Stored Procedures / Functions 下的所有内容,然后通过从数据库更新模型来重新添加它们。我的问题现已解决。

我有这个错误:

The function import *XXX* cannot be executed because it is not mapped to a store function.

当我检查时,我的存储过程已从数据库中删除。我再次创建它,它修复了错误。

当您打开 edmx 资源管理器(图表模式)时,您会在图表的一侧看到模型资源管理器 window。

在函数导入部分尝试找到您存储的函数。右键点击它,然后就可以修改了。

会打开一个Window,然后选择你存储的函数。 That

之后将映射一个函数

瞧,对我有用。

在 devArt、EF 5.0 上使用 oracle db。为我解决的是从模型中删除所述存储过程,然后重新添加它。确保存储过程在数据库中正确编译,否则您将无法选择将其添加到模型中。

对于那些尝试过该解决方案但仍然无效的人,请确保您的过程没有任何无效列名问题或任何其他相关问题。我收到了同样的错误消息,但在我的例子中,它是过程中的无效列名引用。

如果有人仍然遇到同样的问题,请在模型浏览器中执行以下两个步骤

  1. 从模型选项卡中的函数导入中删除函数
  2. 从商店中删除程序

然后像我们最初那样再次添加它

以下步骤为我解决了这个问题: 在 Visual Studio 打开 .edmx 文件设计器。 在设计器中右击 select 模型浏览器。 在模型浏览器上双击实体,编辑函数导入向导打开。 在此向导中,确保正确填充了与存储过程对应的下拉列表。 重建

在这种情况下我通常会做什么: 在记事本++中打开 .edmx 文件 删除对导致问题的已存储 procedure/view/table 的任何引用。 在visual studio(设计器)中打开edmx,再次更新模型。 对于存储过程,删除记事本中的任何引用,然后在 visual studio 更新模型 window 中,SP 将显示在该树中,您可以 select 它。这样,将为该 SP 重新生成代码。