Update an extended asp net identity class' custom fields

Viewed 105

I'm currently building a user admin page for my web app (asp net 4.8 C# and WebForms). I have most of the functionality working, the only part I seem to be stuck on is updating a users info (creating a new user works as expected).

Here's my code extending the base IdentityUser class:

    public class UserHandler : IdentityUser
    {
        public int FacilityID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
        public bool Active { get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreateDate { get; set; }
        public int ModifyBy { get; set; }
        public DateTime ModifyDate { get; set; }
        public DateTime ModifyPasswordDate { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<UserHandler>
    {
        public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { }

        public static ApplicationDbContext Create() { return new ApplicationDbContext(); }

        public class UserManager : UserManager<UserHandler>
        {
            public UserManager(UserStore<UserHandler> store)
               : base(new UserStore<UserHandler>(new ApplicationDbContext())) {  }
        }
     }

Here is the UPDATE function the I'm trying to get working :

        private string SaveUserAndRoles()
        {
            IdentityResult result = new IdentityResult();
            UserManager<UserHandler> manager = new UserManager<UserHandler>(new UserStore<UserHandler>(new ApplicationDbContext()));

            string userID = hdnUserID.Value.ToString();
            Boolean newUser = String.IsNullOrWhiteSpace(userID);

            string status = "";
            string fID = hdnFacilityID.Value;
            int.TryParse(fID, out int facilityID);

            if ( ! newUser ) {
                UserHandler user = IdentityHelper.GetUser(userID);
                    user.UserName = userEmail.Text;
                    user.FirstName = userFirstName.Text;
                    user.LastName = userLastName.Text;
                    user.Email = userEmail.Text;
                    user.Phone = userPhone.Text;
                    user.ModifyBy = 1;
                    user.Active = true;
                    user.ModifyDate = DateTime.Now;
                    user.ModifyPasswordDate = DateTime.Now;

                try { manager.Update(user); }
                catch (Exception ex) { var msg = ex.Message; }
            }
            else { 
                <code for creating new user, which is working>
            }

            try {
                 if (newUser)
                     manager.Create(user, userPassword.Text);
                 else {
                        try { manager.Update(user); }
                        catch (Exception ex) { var msg = ex.Message; }
                 }

                 if (result.Succeeded) {
                     List<string> roles = getRoles();
                     foreach (string userRole in roles)
                         IdentityHelper.AddUserToRole(user.Id.ToString(), userRole);

                     status = user.Id.ToString();
                 }
                 else
                    {
                        status = $"Error: There was a problem saving the user '{userEmail.Text}':<br/>{result.Errors.FirstOrDefault()}";
                    }

                }
                catch (Exception ex) { status = $"Error: There was a problem saving the user '{userEmail.Text}':<br/>{ex.Message}"; }

The part that is failing is when it's trying to do the update: manager.Update(user);

I get the following message:

Attaching an entity of type 'xxx' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

I'm still very new AspNetIdentity, so it wouldn't surprise me if I did something to screw it up. I've googled the error message but couldn't find anything regarding my issue.

any help is GREATLY appreciated!

0 Answers
Related