在 EF 中调用用户定义的函数 - 我做错了什么?
Calling a user defined function in EF - what am I doing wrong?
我已经在 SQL 服务器中声明了一个用户定义的函数。按照说明 here,我将函数刷新到我的 EDMX 中(使用 "Update model from database"),结果是:
<Function Name="uf_GradeToGradeFlag" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="smallint">
<Parameter Name="grade" Type="smallint" Mode="In" />
</Function>
然后我创建了一个静态方法:
[EdmFunction("SchoolManagement.BL", "uf_GradeToGradeFlag")]
public static short GradeToGradeFlag(short grade)
{
throw new NotSupportedException("Direct calls are not supported.");
}
现在,当我在 Linq-to-Entities 调用中调用此方法时,我得到:
System.NotSupportedException: The specified method 'Int16 GradeToGradeFlag(Int16)' on the type 'MyDomain.ModelDefinedFunctions' cannot be translated into a LINQ to Entities store expression.
我怀疑问题出在 EdmFunctionAttribute constructor 中的 namespaceName
参数。但是文档非常模糊。当您谈论 SQL 函数时,"namespace" 到底是什么?
找到答案here:
The first attribute argument is the store namespace - you can find this in your edmx xml file on the Schema element (look for Namespace).
我已经在 SQL 服务器中声明了一个用户定义的函数。按照说明 here,我将函数刷新到我的 EDMX 中(使用 "Update model from database"),结果是:
<Function Name="uf_GradeToGradeFlag" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="smallint">
<Parameter Name="grade" Type="smallint" Mode="In" />
</Function>
然后我创建了一个静态方法:
[EdmFunction("SchoolManagement.BL", "uf_GradeToGradeFlag")]
public static short GradeToGradeFlag(short grade)
{
throw new NotSupportedException("Direct calls are not supported.");
}
现在,当我在 Linq-to-Entities 调用中调用此方法时,我得到:
System.NotSupportedException: The specified method 'Int16 GradeToGradeFlag(Int16)' on the type 'MyDomain.ModelDefinedFunctions' cannot be translated into a LINQ to Entities store expression.
我怀疑问题出在 EdmFunctionAttribute constructor 中的 namespaceName
参数。但是文档非常模糊。当您谈论 SQL 函数时,"namespace" 到底是什么?
找到答案here:
The first attribute argument is the store namespace - you can find this in your edmx xml file on the Schema element (look for Namespace).