在实体框架中调用Scalar值函数6 [英] Call a Scalar valued function in entity framework 6

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

问题描述

如何在实体框架6中调用标量函数?
我尝试了以下代码

How can I call a scalar function in entity framework 6 ? I have tried the following code

        using (MhEntities DContext = new MhEntities())
        {
            var Account_IdParameter = Account_Id.HasValue ? new ObjectParameter("Account_Id", Account_Id) : new ObjectParameter("Account_Id", typeof(long));
            string res = ((IObjectContextAdapter)DContext).ObjectContext.CreateQuery<string>("MoneyforHealthEntities.Fn_LEVEL0_Acount_Id", Account_IdParameter).FirstOrDefault();
            return Convert.ToInt64(res);
        }


推荐答案

不需要使用 ObjectContext 来做到这一点。另外,我不认为你可以简单地传递函数的名称,你需要给它完整的,有效的SQL。

No need to use ObjectContext to do this. Also, I don't think you can simply pass in the name of the function, you need to give it complete, valid SQL.

所以我会尝试这样的东西相反:

So I would try something like this instead:

using (MhEntities DContext = new MhEntities())
{
    string res = DContext.Database.SqlQuery<string>("SELECT MoneyforHealthEntities.Fn_LEVEL0_Acount_Id(@p0)", Account_Id).FirstOrDefault();
    return Convert.ToInt64(res);
}

由于您没有提供任何有关您正在使用的数据库的详细信息,确切的功能定义,上述可能需要进一步调整。但至少应该给你基本的想法。

Since you didn't give any details about which database you are using, or the exact function definition, it's possible that the above may need further tweaking. But it should at least give you the basic idea.

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

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