如何从LINQ到实体服务器端调用Scalar值函数 [英] How to call Scalar-valued function from LINQ to Entities server-side

查看:125
本文介绍了如何从LINQ到实体服务器端调用Scalar值函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个Scalar值函数:

  ALTER FUNCTION [dbo]。[fx_fooFunct] 
(@MyParam varchar(max))
RETURNS varchar(max)
AS
BEGIN
return @MyParam
END

我想从LINQ to Entities查询中调用此函数,并将结果转换为变量:

  let result = this.ObjectContext.ExecuteFunction< string>(SELECT dbo.fx_fooFunct(@MyParam),新的ObjectParameter(MyParam,hello world))。 FirstOrDefault()

但是,当我执行代码时,我收到这个错误:


LINQ to Entities不识别方法
'System.Data.Objects.ObjectResult`1 [System.String]
ExecuteFunction [ String](System.String,
System.Data.Objects.ObjectParameter [])'方法,此方法不能将
转换为存储表达式。




其他信息:



T他是在服务器上运行的所有查询的一部分。

返回所有数据并使用LINQ to Objects不是一个选项,因为性能。



我不知道我有没有返回类型的 ExecuteFunction 正确,但我不知道还有什么可以...
我在做什么错?



修改

在Ladislav Mrnka的回答的帮助下,这里是解决方案:



创建显示SQL函数的帮助方法:

  public class CustomSqlFunctions 
{
[EdmFunction(MyModel.Store,fx_fooFunct)]
public static string FooFunct(string myParam)
{
throw new NotSupportedException呼叫不支持);
}
}

LINQ现在应该读取:

  let result = CustomSqlFunctions.FooFunct(hello world)


解决方案

你的功能是否映射到EDMX?我想你不会。



在设计器和存储过程中运行从数据库更新向导选择您的SQL函数导入并跟随< a href =http://blogs.msdn.com/b/efdesign/archive/2008/10/08/edm-and-store-functions-exposed-in-linq.aspx =noreferrer>这篇文章创建标记为 EdmFunctionAttribute 的帮助程序,以显示LINQ-TO-Entities的SQL函数。



注意:在code-first / fluent-API中不支持SQL函数。您需要使用EDMX映射。



ExecuteFunction 用于调用EDMX映射的功能 - 映射的功能(存储过程的函数导入)。 MSDN表示它也可以调用映射函数,但我不知道如何调用函数import和SQL函数import没有任何。


I have a Scalar-valued function in my DB:

ALTER FUNCTION [dbo].[fx_fooFunct]
  (@MyParam varchar(max))
RETURNS varchar(max)
AS
BEGIN
  return @MyParam
END

I want to call this function from a LINQ to Entities query and get the result into a variable:

let result = this.ObjectContext.ExecuteFunction<string>("SELECT dbo.fx_fooFunct(@MyParam)", new ObjectParameter("MyParam", "hello world")).FirstOrDefault()

But, when I execute the code, I get this error:

LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectResult`1[System.String] ExecuteFunction[String](System.String, System.Data.Objects.ObjectParameter[])' method, and this method cannot be translated into a store expression.

Other Info:

This is part all of a query running on the server.
Returning all the data and using LINQ to Objects is not an option due to performance.

I'm not sure that I have the return type of the ExecuteFunction correct, but I'm not sure what else it could be... What am I doing wrong?

Edit
With the help of Ladislav Mrnka's answer, here is the solution:

Create helper method exposing the SQL function:

public class CustomSqlFunctions
{
    [EdmFunction("MyModel.Store", "fx_fooFunct")]
    public static string FooFunct(string myParam)
    {
        throw new NotSupportedException("Direct calls not supported");
    }
} 

The LINQ should now read:

let result = CustomSqlFunctions.FooFunct("hello world")

解决方案

Do you have your function mapped in EDMX? I guess you don't.

Run Update from database wizard in the designer and under stored procedures select your SQL function to import and follow this article to create helper method marked with EdmFunctionAttribute to expose the SQL function for LINQ-TO-Entities.

Note: SQL functions are not supported in code-first / fluent-API. You need to use mapping with EDMX.

ExecuteFunction is used to call features mapped in EDMX - it expects name of the mapped feature (function imports of stored procedures). MSDN says that it can also call mapped functions but I don't know how - it calls function import and SQL function import doesn't have any.

这篇关于如何从LINQ到实体服务器端调用Scalar值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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