Call a Scalar valued function in entity framework 6

c# entity-framework linq

Question

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);
        }
1
5
7/30/2015 4:38:05 PM

Popular Answer

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.

9
7/31/2015 12:25:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow