I am trying to do a form in mvc to reset a password when forgotten. When the user click forgot password, he will be redirected into a page where he can enter the email address and then if it matched with an email address in the database, a random generated password will be sent to his email.
The problem is occurring when I am trying to save the changes in the database with Entity.SaveChanges(). Here is the exception:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
Below you can find some code into the business logic which i am doing the mentioned above.
public void ForgotPassword(string email)
{
UsersBL ubl = new UsersBL();
string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder password = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < 8; i++)
{
password.Append(valid[rnd.Next(valid.Length)]);
}
string encPass = HashSha512String(password.ToString(), "1");
Account a = new Account();
if (ubl.GetCompanyByEmail(email) != null)
{
a = ubl.GetAccountByCompanyId(ubl.GetCompanyByEmail(email).companyId);
a.AccountPassword = encPass;
ubl.UpdateAccount(a);
}