问题描述
我有一个devexpress GridControl,我正在设置它的数据源如下:
I have a devexpress GridControl for which I am setting it's datasource like so:
var regs = (from vcap in context.chaps
select vcap);
gridControl1.DataSource = new BindingList<chaps>(regs.ToList());
但是当我使用网格时,我添加或删除的行不会被保存,只有更改初始行被保存。
But when I use the grid, rows I add or delete don't get saved, only changes to the initial rows get saved.
如果我这样做:
gridControl1.DataSource = context.chaps.Local;
我没有任何行,而 AddNewRow
甚至没有视觉上添加新行。
I don't get any rows, and AddNewRow
doesn't even add a new row visually.
如果我这样做:
gridControl1.DataSource = context.chaps.ToList();
我得到行,可以保存对它们的更改;行在视觉上被取消,但不在数据库中,并且不能 AddNewRow
。
I get the rows and can save changes to them; rows get deteled visually but not in the db, and can't AddNewRow
.
如果我这样做: / p>
If I do this:
gridControl1.DataSource = context.chaps;
我收到这个例外:
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().
但context.chaps.Local没有一个 ToBindingList
方法。
but context.chaps.Local does not have a ToBindingList
method either.
我不认为这是devexpress的问题,而是我没有得到如何正确设置数据源。有没有办法获得一个 context.chaps.Local.ToBindingList()
等价?
I don't think this is a problem of devexpress, but rather I'm not getting how to set a datasource properly. Is there a way to get a context.chaps.Local.ToBindingList()
equivalent?
推荐答案
context.chaps.Local
是一个 ObservableCollection< T>
。但是 ToBindingList
不是 ObservableCollection< T>
的方法,而是 DbExtensions
:
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;
为了使用这种方法并使用Intellisense查看它,您需要在代码中包含相应的命名空间文件,您尝试调用 ToBindingList()
:
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;
这篇关于数据绑定直接存储查询(DbSet,DbQuery,DbSqlQuery)不支持Entity Framework 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!