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=;Initial Catalog=test;Uid=test;Pwd=111;");
    SqlCommand cmd = new SqlCommand("Select Count(*) from tblemptemp where Name='" + tm.Name + "'", con);
    object result = cmd.ExecuteScalar();
    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;


Sign In to post your comments