Jer*_*ude 5 entity-framework entity-framework-core
是否可以从 EF Core 中的查询接口调用用户定义的 SQL 函数?例如,生成的 SQL 看起来像
select * from X where dbo.fnCheckThis(X.a, X.B) = 1
Run Code Online (Sandbox Code Playgroud)
就我而言,此子句是对其他 Query() 方法调用的补充,因此 FromSQL() 不是一个选项。
我刚刚在这篇文章的帮助下解决了这个问题(H/T @IvanStoev对这个问题的评论)。
在你的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
}
Run Code Online (Sandbox Code Playgroud)
请注意,该函数必须位于DbContext类中,即使它是静态的。
然后创建数据库迁移并在脚本中定义用户函数。
用法:
var query = db.Foos.Where(f => MyDbContext.SatisfiesMyUserFunction(f.FieldValue));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9498 次 |
| 最近记录: |