title: Unable to cast object of type 'System.String' to type 'System.Int32'
body: <p>I am a new developer and brand new in azure SQL database and I've  published an application which is working fine on my local machine. But I am getting casting error when trying to login on publishing application I don't know why.</p>
  <p><img src="https://i.stack.imgur.com/Q74Fd.png" alt="SQL Database Snapshot" /></p>
  <p>This is the error</p>
  <pre><code>    System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.
     at Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
     at Microsoft.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
     at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
     at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
     at MydateAPI.Repositories.Repo.AuthRepository.Login(String username, String password) in C:\MyProjects\MyDate\MydateAPI\Repositories\Repo\AuthRepository.cs:line 22
     at MydateAPI.Controllers.AuthController.Login(UserForLoginDto userForLoginDTO) in C:\MyProjects\MyDate\MydateAPI\Controllers\AuthController.cs:line 67
     at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeActionMethodAsync&gt;g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeNextActionFilterAsync&gt;g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeInnerFilterAsync&gt;g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeFilterPipelineAsync&gt;g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
     at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  </code></pre>
  <p>This is the Login method</p>
  <pre class="lang-cs prettyprint-override"><code>[HttpPost(&quot;login&quot;)]
  public async Task&lt;IActionResult&gt; Login(UserForLoginDto userForLoginDTO)
  {
      //throw new Exception(&quot;Computer says No!&quot;);
  
      var userFromRepo = await _repo.Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);
  
      if (userFromRepo == null)
          return Unauthorized();
  
      var claims = new[]
      {
          new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
          new Claim(ClaimTypes.Name, userFromRepo.Username)
      };
  
      //Created a security Key for the signin credential and encrypted the key
      var key = new SymmetricSecurityKey(Encoding.UTF8
          .GetBytes(_config.GetSection(&quot;AppSettings:Token&quot;).Value));
  
      //Sign in credential
      var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
  
      //Security token descriptor to contain claim, expiry date of token and signin  credential
      var tokenDescriptor = new SecurityTokenDescriptor
      {
          Subject = new ClaimsIdentity(claims),
          Expires = DateTime.Now.AddDays(1),
          SigningCredentials = creds
      };
  
      var tokenHandler = new JwtSecurityTokenHandler();
  
      var token = tokenHandler.CreateToken(tokenDescriptor);
  
      var user = _mapper.Map&lt;UserForListDto&gt;(userFromRepo);
  
      //return the token as an object to the client
      return Ok(new
      {
          token = tokenHandler.WriteToken(token),
          user
      });
  
  }
  </code></pre>
  <p>Login method inside my Repository</p>
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;User&gt; Login(string username, string password)
  {
      //var user = await _context.Users.Include(p =&gt; p.Photos).FirstOrDefaultAsync(x =&gt; x.Username == username);
      var user = await _context.Users.FirstOrDefaultAsync(x =&gt; x.Username == username);
  
      if (user == null)
      {
          return null;
      }
  
      if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
          return null;
  
      return user;
  
  }
  </code></pre>
  <p>The error appear on Line 67 (below) of AuthController Login</p>
  <pre><code>    var userFromRepo = await _repo.Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);
  </code></pre>
  <p>And Line 22 (below) of my Repository Login</p>
  <pre class="lang-cs prettyprint-override"><code>var user = await _context.Users.FirstOrDefaultAsync(x =&gt; x.Username == username);
  </code></pre>
  <p>But I can't see where I am doing the cast of type 'System.String' to type 'System.Int32'.</p>
  <p>I've attached a snapshot of my SQL database in azure ressourceGroup.</p>
  <p>I am able to register a new user but not to login. But in my local machine I can register and login without any error.</p>
  <p>Need help</p>