我在 SQL Server 2005 数据库中有一个返回 bit 类型的自定义函数。我想通过 Entity Framework 调用这个函数。我已经搜索了一下,但没有找到太多有用的信息。
在 LINQ to SQL 中,这非常容易。我只需要将该函数添加到数据上下文模型中,就可以像这样调用它。
bool result = FooContext.UserDefinedFunction(someParameter);
使用Entity Framework,我已经将函数添加到了我的模型中,并且它出现在模型浏览器的SomeModel.Store\Stored Procedures下。
该模型未为该函数生成任何代码,.edmx文件的XML内容如下:
<Function Name="UserDefinedFunction" ReturnType="bit" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="someParameter" Type="int" Mode="In" />
</Function>
我最接近的实现方式是这样的:
bool result = ObjectContext.ExecuteFunction<bool>(
"UserDefinedFunction",
new ObjectParameter("someParameter", someParameter)
).First();
但我收到了以下错误信息:
在容器'FooEntities'中找不到名为“UserDefinedFunction”的函数导入。
姓名已更改以保护无辜者。
tldr:如何使用Entity Framework 4.0调用标量值用户定义的函数?