Calling the Scalar value function in entity structure 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);
        }

      

+3


source to share


2 answers


No need to use ObjectContext

for this. Also, I don't think you can just pass in the function name, you need to provide complete, valid SQL.

So, I would try something like this:



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 did not provide any details about which database you are using or the exact definition of the function, it is possible that the above may require further configuration. But that should at least give you the basic idea.

+8


source


DateTime ServerTime = new ContextDbEntities().Database.SqlQuery<DateTime>("Select getdate();").FirstOrDefault();
MessageBox.Show(ServerTime.ToString());

      



0


source







All Articles