0

I have this winforms application where I am trying to perform an update to a database. The reason for doing it my long way instead of calling the simple validate, endedit and update is that I want to use a custom method on the password field. I am able to add records but not update them.

var users = entities.users.AsEnumerable().Where(x => x.Code.Equals(int.Parse(txtUsersCode.Text))).FirstOrDefault();

                if (users == null)
                {
                    user userToAdd = new user
                    {
                        firstName = txtUsersFName.Text,
                        lastName = txtUsersLName.Text,
                        username = txtUsersUName.Text,
                        password = GlobalClass.HashEncrypt(txtUsersPassword.Text),
                        created = DateTime.Now,
                        companyAllocated = companyAllocatedComboBox.Text
                    };

                    entities.users.Add(userToAdd);
                    entities.SaveChanges();
                    this.usersTableAdapter.Fill(this.eko_payrollDataSet.users);
                }
                else
                {
                    using (eko_payroll_enterpriseEntities ctx = new eko_payroll_enterpriseEntities())
                    {
                        var userToUpdate = ctx.users.Find(users.Code);

                        if (userToUpdate != null)
                        {
                            //entities.Configuration.ValidateOnSaveEnabled = false;
                            userToUpdate.firstName = txtUsersFName.Text;
                            userToUpdate.lastName = txtUsersLName.Text;
                            userToUpdate.username = txtUsersUName.Text;
                            userToUpdate.password = GlobalClass.HashEncrypt(txtUsersPassword.Text);
                            userToUpdate.modified = DateTime.Now;
                            userToUpdate.companyAllocated = companyAllocatedComboBox.Text;

                            entities.SaveChanges();
                        }
                    }
                }
                MessageBox.Show("User details updated successfully");

No error is thrown but the changed/updated fields do not reflect on the database. I.e, nothing happens on update.

How do I fix this?

This didn't help much. How can I update a single field from an entity with Entity Framework?

1 Answer 1

1

Add this before entities.SaveChanges();

entities.Entry(userToUpdate).State = EntityState.Modified;
entities.SaveChanges();

Edit 1: That error may be due to your Find query. You can try doing this before Calling SaveChanges()

var entityKey = entities.users.Create().GetType().GetProperty("PrimaryKey_ID").GetValue(userToUpdate); //PrimaryKey_ID is your Identity key of that entity
entities.Entry(entities.Set<users>().Find(entityKey)).CurrentValues.SetValues(userToUpdate);
entities.SaveChanges();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick reply. After adding the code, I now get "An object with the same key already exists in the ObjectStatemanager. The ObjectStatemanager cannot track multiple objects with the same key."
@KinyanjuiKamau If you are getting that error see this link stackoverflow.com/questions/19593299/…
Had a problem with one of my columns. Great answer.

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.