문제

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();
    }
도움이 되었습니까?

해결책 2

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>

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top