FunctionImport在实体框架4的问题

编程入门 行业动态 更新时间:2024-10-27 04:33:37
本文介绍了FunctionImport在实体框架4的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用实体框架4。

我有一个存储过程,只是更新我的表中的一个值,即应用程序状态ID。所以我创建了一个这样的存储过程:

ALTER PROCEDURE [dbo]。[UpdateApplicationState] b $ b @ApplicationID INT, @ApplicationStateID INT ) AS BEGIN 更新 [应用程序] SET ApplicationStateID = @ApplicationStateID WHERE ApplicationID = @ApplicationID; END

我创建了一个名为UpdateApplicationState的函数导入。我最初将其返回类型设置为null,但是在上下文中未创建它。所以我将其返回类型更改为int。现在它是在上下文中创建的。从我的存储过程中返回某些东西是否明智?

这是我的ApplicationRepository类中的方法:

public void UpdateApplicationState(int applicationID,int applicationStateID) { var result = context.UpdateApplicationState(applicationID,applicationStateID); }

我的看法是这个方法的调用代码:

applicationRepository.UpdateApplicationState(id,newApplicationStateID);

当我运行它,然后我收到以下错误:

由数据提供者返回的数据读取器对于所请求的查询没有足够的列。

任何想法/建议我可以做什么来让这个工作?

谢谢

解决方案

这是因为你实际上没有从你的存储过程中返回任何东西。将下面的行添加到您的SP( @ ROWCOUNT ),并正确执行。

BEGIN ... SELECT @@ ROWCOUNT END

虽然此解决方案将解决您的问题,实际返回SP的受影响行数,但我不清楚为什么这是您的问题:

我最初将其返回类型设置为null,但是在上下文中没有创建它。

当进行功能导入时,可以选择无作为返回类型,它将在ObjectContext上生成一个新方法,返回类型为 INT 。该方法基本上执行在数据源中定义的存储过程;丢弃从函数返回的任何结果;并返回受执行影响的行数。 编辑:为什么没有返回值的函数在POCO场景中被忽略: 通过ADO.NET C#POCO实体生成器进入 ObjectContext T4模板文件,显示为什么在ObjectContext类中看不到您的功能:简单地忽略!他们逃避生成函数的foreach循环中的下一个迭代。 此解决方法是更改​​T4模板,以实际生成无返回类型的函数的方法,或仅返回基于第一个解决方案的内容。

region.Begin(函数导入); foreach(EdmFunction edmFunction in container.FunctionImports) { var parameters = FunctionImportParameter.Create(edmFunction.Parameters,code,ef); string paramList = String.Join(,,parameters.Select(p => p.FunctionParameterType ++ p.FunctionParameterName).ToArray()); //这是为什么忽略没有返回值的函数: if(edmFunction.ReturnParameter == null) { continue; } string returnTypeElement = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage)); ...

I'm using entity framework 4.

I have a stored procedure that just updates one value in my table, namely the application state ID. So I created a stored procedure that looks like this:

ALTER PROCEDURE [dbo].[UpdateApplicationState] ( @ApplicationID INT, @ApplicationStateID INT ) AS BEGIN UPDATE [Application] SET ApplicationStateID = @ApplicationStateID WHERE ApplicationID = @ApplicationID; END

I created a function import called UpdateApplicationState. I had initially set its return type to null, but then it wasn't created in the context. So I changed its return type to int. Now it was created in the context. Is it wise to return something from my stored procedure?

Here is my method in my ApplicationRepository class:

public void UpdateApplicationState(int applicationID, int applicationStateID) { var result = context.UpdateApplicationState(applicationID, applicationStateID); }

Here is my calling code to this method in my view:

applicationRepository.UpdateApplicationState(id, newApplicationStateID);

When I run it then I get the following error:

The data reader returned by the store data provider does not have enough columns for the query requested.

Any idea/advise on what I can do to get this to work?

Thanks

解决方案

It is because you do not actually returning anything from your stored procedure. Add a line like below to your SP (SELECT @@ROWCOUNT), and it will be executing properly.

BEGIN ... SELECT @@ROWCOUNT END

While this solution will address your issue and actually returns the number of effected rows by your SP, I am not clear on why this is an issue for you:

I had initially set its return type to null, but then it wasn't created in the context.

When doing a Function Import, you can select "None" as return type and it will generate a new method on your ObjectContext with a return type of int. This method basically executes a stored procedure that is defined in the data source; discards any results returned from the function; and returns the number of rows affected by the execution. EDIT: Why a Function without return value is ignored in a POCO Scenario: Drilling into ObjectContext T4 template file coming with ADO.NET C# POCO Entity Generator reveals why you cannot see your Function in your ObjectContext class: Simply it's ignored! They escape to the next iteration in the foreach loop that generates the functions. The workaround for this is to change the T4 template to actually generate a method for Functions without return type or just returning something based on the first solution.

region.Begin("Function Imports"); foreach (EdmFunction edmFunction in container.FunctionImports) { var parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef); string paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray()); // Here is why a Function without return value is ignored: if (edmFunction.ReturnParameter == null) { continue; } string returnTypeElement = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage)); ...

更多推荐

FunctionImport在实体框架4的问题

本文发布于:2023-11-12 13:38:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   FunctionImport

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!