在实体框架6中调用标量值函数

Lij*_*iju 3 c# linq entity-framework

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

        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);
        }
Run Code Online (Sandbox Code Playgroud)

sst*_*tan 8

无需使用ObjectContext这样做.另外,我认为你不能简单地传递函数的名称,你需要给它完整,有效的SQL.

所以我会尝试这样的事情:

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);
}
Run Code Online (Sandbox Code Playgroud)

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