0

Im trying to let the user change his email but when i try to update the database i get an exception: Specified method is not supported.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ManageEmail(LocalEmailModel model)
{
    //UserProfile u = new UserProfile();

    if (ModelState.IsValid)
    {
        bool TryPasswordNow;
        var user = Membership.GetUser(User.Identity.Name);

        MembershipUser u = Membership.GetUser(User.Identity.Name);

        try
        {
            TryPasswordNow = WebSecurity.ChangePassword(User.Identity.Name,   model.OldPassword, model.OldPassword);
        }

        catch (Exception)
        {
            TryPasswordNow = false;
        }

        if (TryPasswordNow == true)
        {
            user.Email = model.NewEmail;
            db.SaveChanges();

            u.Email = model.NewEmail;
            Membership.UpdateUser(u);
        }

        return RedirectToAction("Manage", "Account");
    }
    return RedirectToAction("Manage", "Account");
}

As you can see ive tried both db.savechanges and membership.updateuser. The first goes through but doesnt change the email and the second one gives me a exception. Shouldnt both of these work? Why isnt my change registered? Thanks..

tereško
  • 58,060
  • 25
  • 98
  • 150
Reft
  • 2,333
  • 5
  • 36
  • 64

1 Answers1

0

If you are using the SimpleMembership provider (check your web.config), this question is probably a duplicate of another question.

Just to expand on this a bit, the reason why the call to SaveChanges() goes through is simply because the MembershipUser object is not tracked by the Entity Framework, so calling that has no effect on the database.

Community
  • 1
  • 1
rfernandes
  • 1,121
  • 7
  • 9
  • Hi and thanks for your reply. Ok i see, but shouldnt db.SaveChanges(); save my changes? – Reft Feb 13 '14 at 23:11
  • Not in this case - MembershipUser (the object you expect to be saved via the "user" variable) is not an entity tracked by the entity framework - it is just an object like any other, so as far as SaveChanges() is concerned, there's nothing to change. – rfernandes Feb 13 '14 at 23:20