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