Entity Framework 标量函数映射

3
我有一个标量函数:
CREATE FUNCTION [dbo].[CheckLocation]
(
    @locationId Int
)
RETURNS bit
AS
BEGIN
    //code
END

我想在Entity Framework上下文中使用它。

我已经将它添加到EDMX文件的SSDL中:

<Function Name="CheckLocation" ReturnType="bit" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" >
    <Parameter Name="locationId" Type="int" Mode="In" />
</Function>

我还创建了一个带有使用EdmFunctionAttribute修饰的方法的部分类:

public partial class MainModelContainer
{
    [EdmFunction("MainModel.Store", "CheckLocation")]
    public bool CheckLocation(int locationId)
    {
        throw new NotSupportedException("Direct calls not supported");
    }
}

我尝试像这样使用这个函数:
Context.CheckLocation(locationId);

我收到了 NotSupportedException("不支持直接调用")

它在 Select 方法中可以工作,但这并不适合我。

我如何在不使用 Select 方法的情况下调用此函数?

1个回答

2

你需要将其作为选择器进行访问。

var students = context.Locations
    .Select ( new  {  location= CheckLocation(locationId)}):

在EF 4.5中,是否可以在Select之外使用UDF? - DeeRain
1
你应该能够在.Where子句中使用它。 - Brian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接