C# EF Don';无法在BindingSource中使用IQueryable保存
我已经用DbSet属性创建了一个代码优先上下文 我使用Windows窗体。如果我按以下方式绑定:C# EF Don';无法在BindingSource中使用IQueryable保存,c#,entity-framework,datagridview,binding,C#,Entity Framework,Datagridview,Binding,我已经用DbSet属性创建了一个代码优先上下文 我使用Windows窗体。如果我按以下方式绑定: _context.Schedules.Load(); scheduleBindingSource.DataSource = _context.Schedules.Local.ToBindingList(); this.Validate(); scheduleBindingSource.EndEdit(); _context.SaveChanges(); private void schedule
_context.Schedules.Load();
scheduleBindingSource.DataSource = _context.Schedules.Local.ToBindingList();
this.Validate();
scheduleBindingSource.EndEdit();
_context.SaveChanges();
private void scheduleBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
_scheduleAdding = true;
}
private void scheduleBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_scheduleAdding)
{
Schedule s = (Schedule)scheduleBindingSource.Current;
s.EmployeeName = employeeComboBox.Text;
s.From = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, 1);
_context.Schedules.Add(s);
_scheduleAdding = false;
}
}
所有这些都非常有效,当我保存为以下内容时:
_context.Schedules.Load();
scheduleBindingSource.DataSource = _context.Schedules.Local.ToBindingList();
this.Validate();
scheduleBindingSource.EndEdit();
_context.SaveChanges();
private void scheduleBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
_scheduleAdding = true;
}
private void scheduleBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_scheduleAdding)
{
Schedule s = (Schedule)scheduleBindingSource.Current;
s.EmployeeName = employeeComboBox.Text;
s.From = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, 1);
_context.Schedules.Add(s);
_scheduleAdding = false;
}
}
数据持续存在;但当我像这样绑定数据时:
var res = _context.Schedules.Where(k => k.EmployeeName.Equals(employeeComboBox.Text)).ToList();
scheduleBindingSource.DataSource = res;
当我保存数据时,不会发生错误
我认为ToList()方法不太好,但我找不到其他方法来将BindingList连接到上下文中的本地数据集
谢谢
安德里亚你可以试试这个:
_context.Schedules.Where(k => k.EmployeeName.Equals(employeeComboBox.Text)).Load();
scheduleBindingSource.DataSource = _context.Schedules.Local.ToBindingList();
这只会带来符合条件的时间表。当在
Where
方法之后调用Load
方法时,它只会将满足条件的记录带到内存中。稍后,当您调用Local
属性时,它将为您提供一个ObservableCollection
,其中包含当前由DbContext
跟踪的所有对象,这些对象将是您之前加载的元素。最后,当您调用ToBindingList
扩展方法时,它将返回一个与给定的ObservableCollection保持同步的BindingList
导致数据不持久的原因是由DataGridView(或BindingSource)引起的,不会将刚刚添加的行的新距离添加到上下文中
因此,我禁用了AllowUserToAddressRow属性(现在我使用BindingNavigator添加按钮)
并实施了以下两项活动:
_context.Schedules.Load();
scheduleBindingSource.DataSource = _context.Schedules.Local.ToBindingList();
this.Validate();
scheduleBindingSource.EndEdit();
_context.SaveChanges();
private void scheduleBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
_scheduleAdding = true;
}
private void scheduleBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_scheduleAdding)
{
Schedule s = (Schedule)scheduleBindingSource.Current;
s.EmployeeName = employeeComboBox.Text;
s.From = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, 1);
_context.Schedules.Add(s);
_scheduleAdding = false;
}
}
我认为它不起作用,因为如果我执行Load()调用,就不会用数据子集替换_context.Schedules DbSet。结果我得到了和打电话前一样的单词。什么电话?你试过我在回答中分享的代码了吗?我在我的项目中也做了同样的工作,只加载满足条件的实体,而不是所有的实体。在答案的代码中,使用方法(…)获取IQueryable对象,然后对IQueryable对象调用.load(),但不为任何变量赋值。调用_context.Schedule.Local.ToBindingList()时,不会得到第一行代码的结果。请检查本节中的筛选部分。Load
方法可在LINQ之后使用operators@AndreaPiovesan:octavioccl的建议还要求您使用DbContext
的新实例<代码>本地
包含过去加载到上下文中的所有实体,因此如果您已经使用上下文查看所有计划
,则本地
仍将包含所有计划
。