Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

[RESOLVED] Update a record using Entity Framework

$
0
0
I have been using the Entity Framework for about 5 minutes, and I have a question on how I can update my table using it.

I am displaying a combobox so an admin user can select a project id. The application is winforms, clickonce. We used to have project_id in the config file, so every time we wanted users to work with a new project_id, we (IT) would update the app.config and users would download the new application. Now, this project_id is getting moved into a new Settings table I created. It has a column called setting and a column called value and right now, one record with a value of 30 for the setting project_id.

When the app runs, an admin user can select a new value from the combobox. When he does, I want to overwrite the value of 30 with the new selection.

My clumsy attempt looks like this:
Code:

              // If the admin has changed the project_id, save it

                if (comboBox1.SelectedIndex != -1 && comboBox1.SelectedIndex != indexInitialValueCombobox)

                {

                    using (XEntities ctx = new XEntities())

                    {

                        userData u = (userData)comboBox1.SelectedItem;

                        projectIdSetting.value = u.Value.ToString();

                        ctx.SaveChanges();

                    }

                }

I can deal with the mechanics of getting the combobox selection, but is there something I need to do to have an update method created in the entity code? I don't know how SaveChanges() knows what record I'm working on and how it knows it should do an update or an add. I know I sound confused; I am.

Viewing all articles
Browse latest Browse all 2918

Latest Images

Trending Articles



Latest Images