将 SqlFunctions 或 EdmFunctions 的等效集合与 MySql 和实体框架结合使用

发布于 2024-12-25 22:37:59 字数 1132 浏览 3 评论 0原文

我正在使用 Linq to Entities,由 MySql 支持。我希望能够使用各种内置的 MySql 函数,例如 rand。如果我使用 MS SQL Server,我可以使用 SqlFunctions 类,但这不适用于 MySql;我收到错误:

类型“System.Data.Objects.SqlClient.SqlFunctions”上的 Rand() 不能 被转换为 LINQ to Entities 存储表达式。

我已经弄清楚如何在我的数据库上创建一个用户定义的函数来包装内置的 RAND:

CREATE FUNCTION Random ()
RETURNS real NOT DETERMINISTIC
RETURN RAND();

然后我从数据库更新我的模型(我使用的是 .ebmx),并创建一个静态类,如下所示

public static class MyUserFunctions {
    [EdmFunction("MyModelNamespace.Store", "Random")]
    public static double Random() {
        throw new ArgumentNullException();
    }
}

:允许我在 Entities 类的 .Where 子句中调用 MyUserFunctions.Random:

using (MyEntities entities = new MyEntities()) {
    // Yes, I know ORDER BY RAND() is slow
    return entities.products.OrderBy(prod => MyUserFunctions.Random()).Take(4);
}

所以问题是,我可以在不创建愚蠢的包装器 UDF 的情况下执行此操作,而只需使所有内置的 MySql 函数更直接地可用吗(也许通过正确设置 EdmFunctionNamespace 属性(在 SqlFunctions 的情况下,命名空间为 "SqlServer")。

I'm using Linq to Entities, backed by MySql. I'd like to be able to use various built in MySql functions such as rand. If I were using MS SQL Server I could use the SqlFunctions class, but this does not work with MySql; I get the error:

Rand() on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot
be translated into a LINQ to Entities store expression.

I've figured out how the create a user defined function on my database that wraps the built in RAND:

CREATE FUNCTION Random ()
RETURNS real NOT DETERMINISTIC
RETURN RAND();

I then update my model from the database (I'm using an .ebmx), and create a static class like so:

public static class MyUserFunctions {
    [EdmFunction("MyModelNamespace.Store", "Random")]
    public static double Random() {
        throw new ArgumentNullException();
    }
}

And that allows me to call MyUserFunctions.Random within a .Where clause over my Entities class:

using (MyEntities entities = new MyEntities()) {
    // Yes, I know ORDER BY RAND() is slow
    return entities.products.OrderBy(prod => MyUserFunctions.Random()).Take(4);
}

So the question is, can I do this without creating the stupid wrapper UDF, and just make all the built in MySql functions available more directly (perhaps by setting the EdmFunction's Namespace property properly (in the case of SqlFunctions the namespace is "SqlServer").

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2025-01-01 22:37:59

有一些所有提供程序都必须支持的规范函数,请参阅:规范函数,但我猜没有 Random 规范函数,如果有的话Random 在列表中,您只需创建 CLR Method 并通过 EdmFunction 属性和 将其映射到 Random >Edm NamespaceName,并且不需要创建UDF; element 在您的模型中...,如果没有任何 Random 规范函数,您可以使用概念架构定义语言 (CSDL) 创建一个 function 元素,其中包含一个 DefiningExpression 元素,并在此部分中编写您的语句(可能在 TSQL 中),而不是在 DataBase 中定义 UDF 并定义 输入参数返回输入 ,并通过 EdmFunctionCRL 函数 映射到它,并从 linq 查询中调用,请参阅以下内容:调用模型定义的函数查询

there are some Canonical Functions that all the provider must be support those, see this: Canonical Functions, but i guess there is no Random canonical function, if there is Random on the list, you can simply create CLR Method and map it to Random by EdmFunction attribute and Edm NamespaceName, and there is no need to create a UDF and <function> element in your model... , if there is not any Random canonical function, you can create a function element in conceptual schema definition language (CSDL) containing a DefiningExpression element, and write your statement (in TSQL probably) in this section, instead of defining UDF in DataBase, and defining input parameter and return type for it, and map a CRL function by EdmFunction to it, and call from linq query, see this: Call Model-Defined Functions in Queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文