Entity Framework scalar function mapping

ぃ、小莉子 提交于 2019-12-10 20:35:50

问题


I have scalar function:

CREATE FUNCTION [dbo].[CheckLocation]
(
    @locationId Int
)
RETURNS bit
AS
BEGIN
    //code
END

I want to use it in Entity Framework context.

I have added this in the *.edmx file:

<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>

I have also created a partial class with method decorated with EdmFunctionAttribute:

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

I try to use this function like this:

Context.CheckLocation(locationId);

And get NotSupportedException("Direct calls not supported"). It works within Select method, but it does not suit me. Help please! How can I call this function without select method?


回答1:


you need to access it as a select

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


来源:https://stackoverflow.com/questions/12393085/entity-framework-scalar-function-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!