Data Binding Directly to a Store Query (Dbset, Dbquery, Dbsqlquery) Is Not Supported

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported

The error is fairly clear - you can't bind directly to the query results, but need to populate some local collection instead.

The simplest way to do this is to convert it to a List<T>, via ToList():

 ddlCon.DataSource = (from em in dw.Employees
select new { em.Title, em.EmployeeID }).ToList();

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported Entity Framework 5

context.chaps.Local is an ObservableCollection<T>. But ToBindingList is not a method of ObservableCollection<T> but an extension method in DbExtensions:

public static BindingList<T> ToBindingList<T>(
this ObservableCollection<T> source) where T : class;

In order to use this method and see it with Intellisense you need to include the corresponding namespace in the code file where you try to call ToBindingList():

using System.Data.Entity;

Data binding directly to a store query

you need to execute the query before data binding. Using ToList() will force your query to execute.

cmbAmount.DataSource = SourceAmounts.ToList()

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Load not there

I solved it by calling the ToList() method on it.

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported

I managed to fix my problem, I created a new DTO class and edited my controller:

    public IEnumerable<MovieDTO> GetAllMovies()
{
var data = this.Data.Movies.All().Select(x => new MovieDTO
{
Id = x.Id,
Name = x.Name,
ReleaseDate = x.ReleaseDate,
Rating = x.Rating,
Duration = x.Duration,
Director = x.Director,
Writer = x.Writer,
Cost = x.Cost,
Type = x.Type
}).OrderBy(x => x.Id).ToList();

return data;
}

NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery)

As explained in the other post you cannot databind directly to DbSet which is what you are attempting to do. Instead you need to get a create a local collection and populate it with the results of a query.

Here is a very good tutorial.

And an example of the view model you can start with:

using System.Collections.ObjectModel;

namespace TryingWPFWithUltimate
{
class ViewModel:INotifyPropertyChanged
{
private DatabaseContext _databaseContext;

public ViewModel()
{
_databaseContext = new DatabaseContext();
Schools = new ObservableCollection<School>(_databaseContext.Schools);
}

public ObservableCollection<School> Schools { get; set; }

}
}

And the binding:

<ComboBox ItemsSource="{Binding Path=Schools}">

Webforms data binding with EF Code-First Linq query error

It is a long story, but I will try not to make it boring.

From the first version of EF we supported binding directly to queries. We earned enough experience about the pitfalls and confusion that this generated that we decided to explicitly disable it the new API we created for EF 4.1.
The main issue for me was that WinForms and WPF data binding infrastructure assumes data sources are in memory and inexpensive to access. This has resulted in data binding often asking for the binding list more than once. On EF, binding to a reusable query necessarily implied wanting the latest results from the database, therefore we made it so that everytime the binding list is asked for we re-execute the query against the database. This caused at least two query executions everytime anyone bound to a query.

There were a few other aspects of binding to queries that were quite confusing or counterintuitive for many customers. I explore how things used to work in this blog post: http://blogs.msdn.com/b/diego/archive/2008/10/09/quick-tips-for-entity-framework-databinding.aspx

What you are supposed to do with DbContext API is to bind to local data directly and not to queries. For that we expose DbSet.Local which is an ObservableCollection that works pretty well for WPF and the ToBindingList method that wraps the collection in a BindingList for easier consumption in WinForms.

I can see that the exception message could be more explicit about the existence of the local property. I will consider filing a bug for that.

Hope this helps

ADO.Net Data Binding Error to ASP DropDownList

You can't bind to a query, which is usually some IQueryable (at least not anymore, not sure if you could in the past), so you need to bind to some local collection.

var soilList = from SOTYPE in db.SOTYPEs
orderby SOTYPE.SOTYPE_DESC ascending
select SOTYPE.SOTYPE_DESC;

soilList represents a query that will be sent to a database, so if you add ToList() at the end, that will create a local collection of the results for your query, and you will be able to bind to that.

See: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported



Related Topics



Leave a reply



Submit