0

I wanna connect to a Sqlite database with EF Core and followed this tutorial(https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite).

All works well(including insert/delete/update and so on).

There is a DbContext:

public Database.ProductDbContext PDC = new Database.ProductDbContext();

And also I want to bind the dbset PDC.StoreIn to a ItemsControl.

I tried bind it like this but it did not work at all:

It reports an error:

System.Windows.Data Error: 40 : BindingExpression path error: 'PDC+StoreIn' property not found on 'object' ''SelectProduct' (Name='')'. BindingExpression:Path=PDC+StoreIn; DataItem='SelectProduct' (Name=''); target element is 'CollectionViewSource' (HashCode=8674443); target property is 'Source' (type 'Object')

However, if I convert it to a ObservableCollection and bind it, it works. Just like this:

public ObservableCollection OC = new ObservableCollection<Database.StoreIn>(PDC.StoreIn);

<ItemsControl ItemsSource="{Binding OC">
</ItemsControl>

Whereas, as we know, whenever the database insert/update/delete, the ObservableCollection will not update so I have to bind the database directly.

How can I bind the dbset? Thank you.

====================================================

Finally I found another solution more easily but not the answer of duplicate one.

public ObservableCollection<Database.StoreIn> OC
        {
            get {                
                return new ObservableCollection<Database.StoreIn>(PDC.StoreIn);
            }
        }

Just bind the OC.

Whenever you insert/update/delete the database, for example:

PDC.StoreIn.Local.Add(new Database.StoreIn());
                PDC.SaveChanges();
                OnPropertyChanged("OC");

Use a INotifyPropertyChanged, then the UI will changes while you change the database now.

102425074
  • 781
  • 1
  • 7
  • 23
  • Did you try to bind to the `Local` property of the `DbSet`? `{Binding PDC.StoreIn.Local}`. – mm8 Jul 08 '19 at 12:37
  • @mm8 I tried your way and VS reports an error:System.NotSupportedException: 'Data binding directly to a store query is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data to avoid sending a query to the database each time the databound control iterates the data. For WPF bind to 'DbSet.Local.ToObservableCollection()'. For WinForms bind to 'DbSet.Local.ToBindingList()'. For ASP.NET WebForms bind to 'DbSet.ToList()' or use Model Binding.' – 102425074 Jul 08 '19 at 13:18
  • This a pretty clear explanation, isn't it. *Data binding directly to a store query is not supported*. – mm8 Jul 08 '19 at 13:19
  • @mm8 Although it can bind by converting to a list/ObservableCollection. Meanwhile, the problem still here. Whenever I insert/update the database, the UI is binding the list/ObservableCollection, not the database directly. So the UI can not update yet. – 102425074 Jul 08 '19 at 13:40
  • Why would the UI update when you insert an item to the database...? You are binding to an in-memory collection. It's your job to keep this one synchronized with the database. – mm8 Jul 08 '19 at 13:41
  • All right. I always thought that whenever the database changes, the UI will change also by binding. Just as the MVVM, whenever the ViewModel changes, the UI will change also.@mm8 – 102425074 Jul 08 '19 at 13:50
  • @mm8 I found another solution but not the answer to the duplicate one and it works. Please see the question that I edited. – 102425074 Jul 08 '19 at 14:02

0 Answers0