在LINQ to Entities查询中调用自定义标量数据库函数?

Jez*_*Jez 7 c# linq sql-server entity-framework ef-code-first

有没有办法可以将自定义标量数据库函数称为LINQ to Entities查询的一部分?我在网上唯一能找到的就是这个页面:

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/how-to-call-custom-database-functions

但是,此处给出的说明似乎假设您使用的是DB-first方法并讨论修改.edmx文件.如果您使用代码优先方法呢?我希望能够写出这样的东西:

var result = await (
    from itm in _itemDataContext.Items
    where
        itm.QryGroup1 == "Y" &&
        _itemDataContext.Dbo_MyCustomScalarIntFn(itm.QryGroup2) > 0
).ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)

Jez*_*Jez 3

事实证明,此功能并未内置于实体框架中,因此我必须使用 NuGet 包(CodeFirstFunctions)来获取它:


项目数据上下文

[DbFunction("CodeFirstDatabaseSchema", "fn_IsCorrectProduct")]
bool Fn_IsCorrectProduct(string companyID, string itemCode);
Run Code Online (Sandbox Code Playgroud)

项目数据上下文

[DbFunction("CodeFirstDatabaseSchema", "fn_IsCorrectProduct")]
public bool Fn_IsCorrectProduct(string companyID, string itemCode)
{
    // UDF is described in DbFunction attribute; no need to provide an implementation...
    throw new NotSupportedException();
}
Run Code Online (Sandbox Code Playgroud)