Reputation: 2731
I don't know how to use DbContext to show data in a Wpf Data Control as the ListView, ListBox or DataGrid.
I'm using the Database first approach, with VS2012.
I have a Database with one table (the simplest case).
Then I added an ADO Net Entity Data Model, it added several classes to my project, among them is OneTableEntities : DbContext:
Now I have a ListBox in a Wpf Window, what do I bind the listbox.ItemsSource to? I tried "listBox.ItemsSource = context.MyTables;" but I get this error:
Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().
Where is that Load operation?, How can I populate a local store?
Upvotes: 1
Views: 3254
Reputation: 236218
As error message says, you cannot bind control to query. Use ToList() to execute database query and save results to in-memory collection. Then you will be able to bind this collection to listbox:
listBox.ItemsSource = context.MyTables.ToList();
Another option you can find directly in error message:
context.MyTables.Load();
listBox.ItemsSource = context.MyTables.Local;
Upvotes: 2