title: Entity Framework query - Must declare the scalar variable
body: <p>This project is coded in c# with ASP.NET MVC.</p>
  
  <p>Hello, I created database with Entity Framework and add some data. Now I want to fetch that data and put it inside variable. </p>
  
  <p>This is how I  fetch data and it worked:</p>
  
  <pre><code>string hash = db.Database.SqlQuery&lt;string&gt;("Select PasswordHash From Users where Username = 'Paul'").FirstOrDefault&lt;string&gt;();
  </code></pre>
  
  <p>When I want to put variable into query like that:</p>
  
  <pre><code>    string hash = db.Database.SqlQuery&lt;string&gt;("Select PasswordHash From Users where Username = @username").FirstOrDefault&lt;string&gt;();
  </code></pre>
  
  <p>It is not working and the error message says that:</p>
  
  <blockquote>
    <p>'Must declare the scalar variable "@username".'</p>
  </blockquote>
  
  <p>Is there any way to fetch data with variable inside query with entity framework? </p>
  
  <p>I read how to do it with standard sql connection by adding parameteres like:</p>
  
  <pre><code>cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
  </code></pre>
  
  <p>Thanks for help.</p>