0

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);

        }
Thiago Ferreira
  • 661
  • 3
  • 19
  • Poiisble duplicate of [entity object cannot be referenced by multiple instances of IEntityChangeTracker](http://stackoverflow.com/questions/10191734/entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetracker) – stuartd Mar 11 '16 at 13:59
  • 1
    can u show the methods GetAccountByCompanyId and UpdateAccount? – Raphael Ribeiro Mar 11 '16 at 14:04
  • 1
    you are using mutiple datacontext entity, so you have to detach entities of that others and attach it to the datacontext whom do SaveChanges – Byron Mar 11 '16 at 14:42
  • can you show how i can detach entities cause i have tried to do it but with no success – Keith Cutajar Mar 11 '16 at 16:22

0 Answers0