实体框架核心:用户定义的SQL函数 [英] Entity Framework Core: User Defined SQL Functions

查看:102
本文介绍了实体框架核心:用户定义的SQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从EF Core中的查询接口调用用户定义的SQL函数?例如,生成的SQL看起来像

Is it possible to invoke a user-defined SQL function from the query interface in EF Core? For example, the generated SQL would look like

select * from X where dbo.fnCheckThis(X.a, X.B) = 1

就我而言,此子句是对其他Query()方法调用的补充,因此FromSQL()不是选项。

In my case, this clause is in addition to other Query() method calls so FromSQL() is not an option.

推荐答案

我刚刚在本文(H / T @IvanStoev 对问题的评论)。

I just managed this with help from this article (H/T @IvanStoev for his comment on the question).

在您的 DbContext 类:

[DbFunction("my_user_function_name")]
public static bool SatisfiesMyUserFunction(int i)
{
    throw new Exception(); // this code doesn't get executed; the call is passed through to the database function
}

请注意,函数即使它是静态的,也必须在 DbContext 类中。

然后创建数据库迁移并在脚本中定义用户功能。

Then create a database migration and define the user function in the script.

用法:

var query = db.Foos.Where(f => MyDbContext.SatisfiesMyUserFunction(f.FieldValue));

这篇关于实体框架核心:用户定义的SQL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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