将 SqlFunctions 或 EdmFunctions 的等效集合与 MySql 和实体框架结合使用
我正在使用 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 函数更直接地可用吗(也许通过正确设置 EdmFunction
的 Namespace
属性(在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些所有提供程序都必须支持的
规范函数
,请参阅:规范函数,但我猜没有Random
规范函数,如果有的话Random
在列表中,您只需创建CLR Method
并通过EdmFunction
属性和将其映射到
Random
>EdmNamespaceName
,并且不需要创建UDF
和; element
在您的模型中...,如果没有任何Random
规范函数,您可以使用概念架构定义语言 (CSDL) 创建一个function
元素,其中包含一个DefiningExpression
元素,并在此部分中编写您的语句(可能在 TSQL 中),而不是在DataBase
中定义UDF
并定义输入参数
和返回输入
,并通过EdmFunction
将CRL 函数
映射到它,并从linq
查询中调用,请参阅以下内容:调用模型定义的函数查询there are some
Canonical Functions
that all the provider must be support those, see this: Canonical Functions, but i guess there is noRandom
canonical function, if there isRandom
on the list, you can simply createCLR Method
and map it toRandom
byEdmFunction
attribute andEdm
NamespaceName
, and there is no need to create aUDF
and<function> element
in your model... , if there is not anyRandom
canonical function, you can create afunction
element in conceptual schema definition language (CSDL) containing aDefiningExpression
element, and write your statement (in TSQL probably) in this section, instead of definingUDF
inDataBase
, and defininginput parameter
andreturn type
for it, and map aCRL function
byEdmFunction
to it, and call fromlinq
query, see this: Call Model-Defined Functions in Queries