Linq to Entities 中的 SQL 用户定义函数

发布于 2025-01-04 05:41:08 字数 1181 浏览 3 评论 0原文

我正在尝试使用实体框架在 linq 语句中调用用户定义的 sql 函数。该函数名为 GetXml,采用 2 个名为“data”和“path”的字符串并返回一个字符串。我的数据模型称为 XmlDataModel。我将函数定义包含在 edmx 文件中,如下所示:

    <Function Name="GetXml" ReturnType="varchar(max)"   Schema="dbo">
      <Parameter Name="path" Type="varchar(max)" Mode="In" />
      <Parameter Name="data" Type="varchar(max)" Mode="In" />
    </Function>

我的代码中有以下方法声明:

    [EdmFunction("XmlDataModel.Store", "GetXml")]
    public string GetXml(string path, string data)
    {
        throw new NotSupportedException("This method can only be used in a LINQ-toEntities query");
    }

以及以下 linq 语句:

var test = from e in this.ObjectContext.Events 
           where GetXml("a", "b") == "test" select e.EventSpecificData;

我的问题是,当我尝试查看结果集时,我收到以下消息:

指定方法 'System.String GetXml(System.String, 类型“RIAServicesLibrary1.Web.XmlDomainService”上的 System.String)' 无法转换为 LINQ to Entities 存储表达式,因为 调用它的实例不是 ObjectContext 将评估使用它的查询。

它似乎认识到我已经定义了该方法,因为当我取出 [EdmFunction] 标签时,我收到了不同的消息。有什么想法吗?

I am trying to call a user defined sql function in my linq statement using entity framework. The function is called GetXml, and takes 2 strings called "data" and "path" and returns a string. My Data Model is called XmlDataModel. I included the function definition in the edmx file as so:

    <Function Name="GetXml" ReturnType="varchar(max)"   Schema="dbo">
      <Parameter Name="path" Type="varchar(max)" Mode="In" />
      <Parameter Name="data" Type="varchar(max)" Mode="In" />
    </Function>

I have the following declaration of the method in my code:

    [EdmFunction("XmlDataModel.Store", "GetXml")]
    public string GetXml(string path, string data)
    {
        throw new NotSupportedException("This method can only be used in a LINQ-toEntities query");
    }

And the following linq statement:

var test = from e in this.ObjectContext.Events 
           where GetXml("a", "b") == "test" select e.EventSpecificData;

My problem is that when I attempt to view the result set, I get the following message:

The specified method 'System.String GetXml(System.String,
System.String)' on the type 'RIAServicesLibrary1.Web.XmlDomainService'
cannot be translated into a LINQ to Entities store expression because
the instance over which it is invoked is not the ObjectContext over
which the query in which it is used is evaluated.

It seems to be recognizing that I have the method defined, because when I take out the [EdmFunction] tag I get a different message. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文