Webforms数据绑定与EF Code-First Linq查询错误 [英] Webforms data binding with EF Code-First Linq query error

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

问题描述

在此示例中,这里,Scott显示对dbContext进行Linq查询,并将结果直接绑定到GridView以显示产品列表。他的例子是使用C代码的第一个东西。

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.

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

However, when I try do do the same thing using the latest version of EntityFramework 4.1, I get the following error:


数据绑定到商店查询(DbSet,DbQuery,DbSqlQuery)不支持
。而是用数据填充DbSet,例如
在DbSet上调用Load,然后绑定到本地数据。

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.

我看到DBQuery对象在实现IListSource.GetList()时有意引发这个错误,它用于数据绑定。

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

任何想法为什么他的例子工作?顺便说一下,我知道我可以通过放入一个 projects.ToList()来完成这项工作。我的主要问题是发布版本中是否改变了这种类型的东西不再有效,或者我是否缺少可以解决这个错误的地方。

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.

从第一个版本的EF,我们支持直接绑定到查询。我们获得了关于陷阱和混淆的足够经验,这导致我们决定明确禁用它为EF 4.1创建的新API。
我的主要问题是WinForms和WPF数据绑定基础架构假定数据源在内存中,访问成本低廉。这导致数据绑定通常多次询问绑定列表。在EF上,绑定到可重用的查询必然意味着想要数据库的最新结果,因此我们做到这一点,每当绑定列表被要求我们重新对数据库执行查询。这导致每次任何人绑定查询的至少两次查询执行。

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.

还有一些其他方面的绑定到对许多客户来说相当混乱或违反直觉的查询。我探索了在这篇博文中如何工作: http://blogs.msdn.com/b/diego/archive/2008/10/09/quick-tips-for-entity-framework-databinding.aspx

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

您应该使用DbContext API做的是直接绑定到本地数据而不是查询。为此,我们公开了DbSet.Local,这是一个ObservableCollection,它适用于WPF和ToBindingList方法,该方法将收集包装在BindingList中,以便在WinForms中更容易地使用。

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.

希望这有帮助

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

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