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

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

问题描述

在这个例子中 此处,Scott 展示了对 dbContext 执行 Linq 查询并将结果直接绑定到 GridView 以显示产品列表.他的例子是使用 Code First 的 CTP4 版本.

但是,当我尝试使用最新版本的 EntityFramework 4.1 做同样的事情时,我收到以下错误:

<块引用>

直接绑定到存储查询(DbSet、DbQuery、DbSqlQuery)的数据是不支持.而是用数据填充 DbSet,例如通过调用 DbSet 上的 Load,然后绑定到本地数据.

我看到 DBQuery 对象在它的 IListSource.GetList() 实现中故意抛出这个错误,它用于数据绑定.

有什么想法为什么他的例子有效?顺便说一句,我知道我可以通过放入 projects.ToList() 来完成这项工作.我的主要问题是发布版本中是否有某些更改使此类事情不再起作用,或者我是否遗漏了可以解决此错误的某些内容.

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

MyDbContext db = new MyDbContext();var projects = from p in db.Projects其中 p.AnotherField == 2选择 p;grdTest.DataSource = 项目;grdTest.DataBind();

解决方案

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

从 EF 的第一个版本开始,我们支持直接绑定到查询.我们获得了足够的关于由此产生的陷阱和混乱的经验,因此我们决定明确禁用我们为 EF 4.1 创建的新 API.对我来说,主要问题是 WinForms 和 WPF 数据绑定基础结构假设数据源在内存中并且访问成本低.这导致数据绑定经常不止一次地要求绑定列表.在 EF 上,绑定到可重用查询必然意味着需要数据库中的最新结果,因此我们做到了每次要求绑定列表时,我们都会针对数据库重新执行查询.每当有人绑定到查询时,这都会导致至少执行两次查询.

绑定到查询的其他一些方面对许多客户来说非常令人困惑或违反直觉.我在这篇博文中探讨了过去的工作方式: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 中使用.

我可以看到异常消息可以更明确地说明本地属性的存在.我会考虑为此提交一个错误.

希望能帮到你

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

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

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