1

I have a winforms app with a group of text boxes for an entity, let's call the entity Product

One of my textboxes is hidden because it holds a foreign key to another database object Business Unit. I populate the hidden textbox using a combobox that looks up values on the parent table. When the selection changes, so does the value in the hidden textbox.

private void businessUnitComboBox_SelectionChangeCommitted(object sender, EventArgs e)
    {
        this.businessUnitIdTextBox.Text = this.businessUnitComboBox.SelectedValue.ToString();

        this.businessUnitComboBox.Focus();
    }

Problem is after calling SaveChanges on my context, this change on the hidden textbox is not persisted. Oddly, if I update any of the other Product textboxes, they save just fine.

The textboxes were added to the project with standard drag and drag drop from Visual Studio's GUI, and a bindingsource was created when adding automatically.

My entities implement INotifyPropertyChanged using fody-propertychanged.

I am struggling to find the issue here. Creating new records works fine, but updating that one Foreign Key value never has.

Is this because it is a navigation property and needs to be handled differently, or are there other possibilities as to why the changes are not persisted? Any help is much appreciated.

wesmantooth
  • 625
  • 2
  • 11
  • 29
  • Can we see your Product model class? – samiz Aug 24 '15 at 21:27
  • I was able to solve this issue assigning directly to the foreign key object instead of the Foreign Key Id. The `Product` class is POCO with fody-INotifyPropertyChanged magic included. But I do use a Presenter as intermediary between form and context. I am now passing the `BusinessUnitBindingSource.Current` into the presenter and setting the navigation property instead of Id. – wesmantooth Aug 25 '15 at 17:30

1 Answers1

1

May be it is because of lacking of this.Validate() in your Save method. Put this code in save method of your form.

this.Validate();
this.yourBindingSource.EndEdit();
yourDbContext.SaveChanges();

The default behavior of data binding is OnValidation.

To change this behavior you can go to your textbox properties, open databinding part, open advanced, Change Data Source Update Mode to OnPropertyChanged.

So the code of your databinding in designer generated codes will be like this:

this.nameTextBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.productBindingSource, "Category.Name", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

In this case you don't need to use this.Validate before saving changes and you can simply use

this.yourBindingSource.EndEdit();
yourDbContext.SaveChanges();
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thank you for the suggestions and explanation of DataSourceMode. I am new to the framework and was not aware of these configurations. Unfortunately adding these lines did not change my situation. I was able to solve this issue assigning directly to the foreign key object instead of the Foreign Key Id. So I am using `Product.BusinessUnit = BusinessUnitBindingSource.Current` instead of `Product.BusinessUnitId = (int)this.businessUnitIdTextBox.Text` – wesmantooth Aug 25 '15 at 17:27