gpt4 book ai didi

c# - 从 EntityFramework 调用存储过程时出错

转载 作者:IT王子 更新时间:2023-10-29 04:10:44 26 4
gpt4 key购买 nike

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

我已按照以下步骤操作:

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

enter image description here

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

enter image description here

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

enter image description here

在“函数导入”部分中,所有参数均设置为“输入”,而“MaxReference”应标记为“输出”。我该如何更改它?

enter image description here

虽然这两个问题我已经执行了代码:

enter image description here

我得到了以下异常:

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

和内部异常:

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

enter image description here

最佳答案

您可能需要引用这篇博文: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 .

关于c# - 从 EntityFramework 调用存储过程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com