0

Im trying to update a NavigationProperty of a single entity object but its not updating after SaveChanges.

It only works when the object entity is not bound to any UI element through a property, i mean, if I bind a property to the UI, and change a navigatio property, the property is changed normally but its not updating on database.

Before trying to do it via binding it was working perfectly...

Code:

public TProd_NCMProd ItemAt
    {
        get { return itemAt; }
        set
        {
            itemAt = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ItemAt"));
        }
    }
...
...
...
private void save()
    {
            //ItemAt.TProd_NCMGrupo is my navigation property
            ItemAt.TProd_NCMGrupo = ((TProd_NCMGrupo)cb_ncmGrupo.SelectedItem);

            itemAtBo.update(ItemAt);
            itemAtBo.saveChanges();
    }
...

The Update and Savechanges methods (DAO layer):

 public void update(T pEntity)
    {
        entidades.ApplyCurrentValues<T>(pEntity.GetType().Name, pEntity);
    }

 public void saveChanges()
    {
        entidades.SaveChanges();
    }
Rafael A. M. S.
  • 637
  • 1
  • 6
  • 27

2 Answers2

1

You need to update the navigation entity directly to the context. Don't update your item.MyNavObject then send item to the context. Send MyNavObject to the context for updating.

Unit of Work and Repository patterns will help you solve a lot of your issues.

Steve Stokes
  • 1,200
  • 18
  • 36
  • Could you be more specific? Thanks for answering – Rafael A. M. S. Jun 21 '13 at 20:12
  • TProd_NCMGrupoBo.Update(TProd_NCMGrupo) is what you should be calling. You're calling itemAtBo.update(ItemAt) which is the wrong entity to update in the context. – Steve Stokes Jun 21 '13 at 20:15
  • But the itemAt is the element that i should update, since the itemAt has the foreign key to the TProc_NCMGrupo... correct? – Rafael A. M. S. Jun 21 '13 at 20:18
  • Here you're updating the TProd_NCMGrupo entity: ItemAt.TProd_NCMGrupo = ((TProd_NCMGrupo)cb_ncmGrupo.SelectedItem);, NOT the ItemAt entity. – Steve Stokes Jun 21 '13 at 20:20
  • no, im updating the reference to NCMGrupo || ItemAt.TProd_NCMGrupo = the NCMGrupo i selected on a combobox. So the ItemAt will reference the NCMGrupo – Rafael A. M. S. Jun 21 '13 at 20:22
  • Don't update the reference, update the TProd_NCMGrupo entity directly. By updating ItemAt, the context looks for changes on ItemAt only, not in the navigation properties. Here's a good example of Repository pattern with an MVC site: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application – Steve Stokes Jun 21 '13 at 20:27
  • Even if the ItemAt has many-to-one to NCMGrupo? It shouldn't receive the foreign key to the object, i mean, looking at the logic behind it the object that should update is the object that receives the foreign key, not the pointed reference... im wrong? – Rafael A. M. S. Jun 21 '13 at 20:32
0

Instead of manually setting the reference

ItemAt.TProd_NCMGrupo = ((TProd_NCMGrupo)cb_ncmGrupo.SelectedItem);

I just set the combobox to be bound to the navigation property of my ItemAt->TProd_NCMGrupo so when I change the combobox selection, the navigation property changes too.

Xaml

<combobox ItemsSource="{Binding ItemsCb}" SelectedItem="{Binding Path=ItemAt.TProd_NCMGrupo, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
...
</combobx>
Rafael A. M. S.
  • 637
  • 1
  • 6
  • 27