gpt4 book ai didi

c# - 在 Entity Framework 4 中调用用户定义的函数

转载 作者:IT王子 更新时间:2023-10-29 04:33:28 25 4
gpt4 key购买 nike

我在 SQL Server 2005 数据库中有一个用户定义的函数,它返回一个位。我想通过 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();

但我收到以下错误信息:

The FunctionImport 'UserDefinedFunction' could not be found in the container 'FooEntities'.

名称已更改以保护无辜者。

tldr:如何使用 Entity Framework 4.0 调用标量值用户定义函数?

最佳答案

我终于解决了 :D 对于标量函数,您可以附加 FROM {1} 子句。

bool result = FooContext.CreateQuery<bool>(
"SELECT VALUE FooModel.Store.UserDefinedFunction(@someParameter) FROM {1}",
new ObjectParameter("someParameter", someParameter)
).First();

这绝对是在 EF 上使用 LINQ to SQL 的情况。

关于c# - 在 Entity Framework 4 中调用用户定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3500509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com