简体   繁体   中英

Updating database fields with Entity Framework 5

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. Ie, 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM