0

I am struggling with this error which is highlighting on:
var account = db.Accounts.SingleOrDefault(a => a.Username.Equals(username));.
The error message I am getting is:

Unable to cast object of type 'System.Int32' to type 'System.String'.

My data type is not int. I am wondering where the Int32 part is coming from. Below is my code:

[HttpPost]
[Route("process")]
public IActionResult Process(string username, string password)
{
    var account = processLogn(username, password);
    if (account != null)
    {
        securityManager.SignIn(this.HttpContext, account);
        return RedirectToAction("index","dashboard", new { area = "admin" });
        //return View("Index");
    }
    else
    {
        ViewBag.error = "Invalis Account";
        return View("Index");
    }
}

private Account processLogn(string username, string password)
{
    var account = db.Accounts.SingleOrDefault(a => a.Username.Equals(username));
    if (account != null)
    {
        if (BCrypt.Net.BCrypt.Verify(password, account.Password))
        {
            return account;
        }
    }
    return null;
}

Here is my class:

[Table("Account")]
public class Account {
    public Account()
    {
        RoleAccounts = new HashSet<RoleAccount>();
    }
    
    [Key]
    public string Id { get; set; }

    public string Username { get; set; }
    public string Password { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public bool Status { get; set; }
    public virtual ICollection<RoleAccount> RoleAccounts { get; set; }
}

Table description:

Username varchar(250) Checked
1
  • Can you provide the class definition for Account and the underlying table definition for Accounts? It seems as if your table is defined as Username being a numeric type. Commented Sep 13, 2020 at 12:24

1 Answer 1

1

The problem can be that Id column in the database is of type int and in your Account class it has a type string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.