在实体调用用户自定义函数框架4 [英] Calling user defined functions in Entity Framework 4

查看:182
本文介绍了在实体调用用户自定义函数框架4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它返回一个有点SQL Server 2005数据库用户定义的函数。我想通过实体框架调用这个函数。我一直在寻找周围并没有多少运气。

I have a user defined function in a SQL Server 2005 database which returns a bit. I would like to call this function via the Entity Framework. I have been searching around and haven't had much luck.

在LINQ to SQL中,这是可耻的容易,我想补充的功能数据上下文模型,我可以这样称呼它。

In LINQ to SQL this was obscenely easy, I would just add the function to the Data context Model, and I could call it like this.

bool result = FooContext.UserDefinedFunction(someParameter);

使用实体框架

,我已经加入的功能我的模型,它SomeModel.Store \\存储过程下出现在模型浏览器。

Using the Entity Framework, I have added the function to my Model and it appears under SomeModel.Store\Stored Procedures in the Model Browser.

该模型生成任何code的作用,为.edmx文件的XML包含:

The model has generated no code for the function, the XML for the .edmx file contains:

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

我能得到最接近的是这样的:

The closest I could get was something like this:

bool result = ObjectContext.ExecuteFunction<bool>(
    "UserDefinedFunction",
    new ObjectParameter("someParameter", someParameter)
).First();

但我得到了以下错误消息:

But I got the following error message:

该FunctionImport
  UserDefinedFunction不能
  在容器FooEntities'找到。

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

名称已被更改,以保护无辜。

Names have been changed to protect the innocent.

tldr:我如何使用调用实体框架4.0标量值用户自定义函数

推荐答案

我终于工作了。:D对于标量函数可以追加FROM {1}条款

I have finally worked it out :D For scalar functions you can append the FROM {1} clause.

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

这肯定是使用LINQ to SQL在EF的情况。

This is definitely a case for using LINQ to SQL over EF.

这篇关于在实体调用用户自定义函数框架4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆