【发布时间】:2011-03-30 20:30:04
【问题描述】:
我在 SQL Server 2005 数据库中有一个用户定义的函数,它返回一点。我想通过实体框架调用这个函数。我一直在四处寻找,但运气不佳。
在 LINQ to SQL 中这非常简单,我只需将函数添加到数据上下文模型中,就可以这样调用它。
bool result = FooContext.UserDefinedFunction(someParameter);
使用实体框架,我已将函数添加到我的模型中,它出现在模型浏览器的 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();
但我收到以下错误消息:
函数导入 'UserDefinedFunction' 不能是 在容器“FooEntities”中找到。
为了保护无辜者,改名了。
tldr:如何使用 Entity Framework 4.0 调用标量值用户定义函数?
【问题讨论】:
标签: c# entity-framework linq-to-entities entity-framework-4