You must Sign In to post a response.
  • Category: ASP.NET

    Entity Framework Code-First Execute Scalar-Valued Functions

    Please guide how to convert the below code to corresponding entity framework code [considering dbempEntities obj = new dbempEntities ();]

    SqlConnection con = new SqlConnection("Data Source=111.178.0.113;Initial Catalog=test;Uid=test;Pwd=111;");
    SqlCommand cmd = new SqlCommand("Select Count(*) from tblemptemp where Name='" + tm.Name + "'", con);
    con.Open();
    object result = cmd.ExecuteScalar();
    con.Close();
    if (result != null)
    {
    IsEmployeeExists = Convert.ToInt32(result.ToString()) > 0;
    }
  • #763481
    You can use either StoredProcedure or SqlQuery


    Using Stored Procedure
    string outParam = obj.Database.ExecuteSqlCommand("exec storedProc @Firstname={0},firstName);


    or via Sql Query

    IEnumerable<string> test=obj.Database.SqlQuery<string>("YourSqlQuery @param1 ={0}", paramValue);
    string outMessage=string.Empty;
    foreach (string s in test)
    {
    outMessage = s;
    }
    return outMessage;

    cheers!


Sign In to post your comments