Web表单数据与EF code-首先Linq查询绑定错误 [英] Webforms data binding with EF Code-First Linq query error

查看:368
本文介绍了Web表单数据与EF code-首先Linq查询绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个例子中<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-$c$c-first-with-an-existing-database.aspx\">here,斯科特显示做对的DbContext LINQ查询,并将结果直接绑定到一个GridView显示产品列表。他的例子是使用的code首先东西CTP4版本。

然而,当我尝试使用最新版本的4.1的EntityFramework也做同样的事情,我得到以下错误:


  

数据直接绑定到一个存储查询(DbSet,的DBQuery,DbSqlQuery)是
  不支持。代替用数据填充DbSet,例如通过
  呼吁DbSet负载,然后绑定到本地数据。


我看到的DBQuery对象在其实施IListSource.GetList(),它是在数据绑定使用的故意引发此错误。

任何想法,为什么他的榜样的作品?顺便说一句,我知道我可以通过将在 projects.ToList使这项工作()。我的主要问题是,一些在发行版本,使得这种类型的东西不再起作用,还是我失去了一些东西的地方,可以解决此错误。修改

仅供参考,我指的是code是这样的:

  MyDbContext DB =新MyDbContext();VAR项目=从db.Projects p
               其中,p.AnotherField == 2
               器选择p;grdTest.DataSource =项目;
grdTest.DataBind();


解决方案

这是一个很长的故事,但我会尽量不让它无聊。

从EF的第一个版本,我们支持直接绑定到查询。我们赚了陷阱和困惑,这产生的,我们决定来明确禁止它,我们为EF 4.1创建的新API足够的经验。
对我来说,主要的问题是,WinForms和WPF数据绑定基础设施假设数据源在内存和廉价接入。这导致了数据绑定经常询问的结合列表不止一次。在EF,绑定到一个可重用的查询必然意味着从数据库中想要的最新成果,因此我们把它使每次绑定列表要求我们重新对数据库执行查询。这导致至少有两个查询执行每次有人绑定到一个查询。

有绑定到是相当混乱,或违反直觉的为众多客户询问一些其他方面。我探究事物是如何使用这个博客帖子的工作:<一href=\"http://blogs.msdn.com/b/diego/archive/2008/10/09/quick-tips-for-entity-framework-databinding.aspx\">http://blogs.msdn.com/b/diego/archive/2008/10/09/quick-tips-for-entity-framework-databinding.aspx

你所应该具有的DbContext API做的是直接和不绑定到本地数据的查询。为此,我们揭露DbSet.Local这是工作pretty以及WPF和一个封装中的BindingList集合中的WinForms更容易消耗ToBindingList方法一个ObservableCollection。

我可以看到异常消息可能会更加明确的本地属性的存在。我会考虑申请为一个错误。

希望这有助于

In this example here, Scott shows doing a Linq query against the dbContext and binding the result directly to a GridView to show a list of products. His example is using the CTP4 version of the Code First stuff.

However, when I try do do the same thing using the latest version of EntityFramework 4.1, I get the following 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.

I see that the DBQuery object is throwing this error on purpose in its implementation of IListSource.GetList(), which is used in databinding.

Any ideas why his example works? By the way, I know that I can make this work by putting in a projects.ToList(). My main question is whether something changed in the release version that makes this type of thing no longer work, or whether I'm missing something somewhere that can work around this error.

Just for reference, I'm referring to code like this:

MyDbContext db = new MyDbContext();

var projects = from p in db.Projects
               where p.AnotherField == 2
               select p;

grdTest.DataSource = projects;
grdTest.DataBind();

解决方案

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

这篇关于Web表单数据与EF code-首先Linq查询绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆