datagridview绑定到不更新数据库的实体

Ero*_*ocM 9 c# entity-framework datagridview winforms

我正在从实体对象填充网格,它正在显示数据.当我进行更改并将其保存回来时,没有任何更新.

这是我的代码:

在我的加载事件中:

  var query = from c in _entities.PaymentTypes
              where c.CorporationId == _currentcorp.CorporationId
              select
                new DataBindingProjection
                  {
                    PaymentTypeId = c.PaymentTypeId,
                    CorporationId = c.CorporationId,
                    TokenId = c.TokenId,
                    IsActive = c.IsActive,
                    Description = c.Description,
                    CashChargeCodeType = c.CashChargeCodeType,
                    SortOrder = c.SortOrder,
                    ExcludeCreditCode = c.ExcludeCreditCodes,
                    IsUpdated = c.IsUpdated,
                    IsAdded = c.IsAdded,
                    ClearUpdatedAndAdded = c.ClearUpdateAndAdded
                  };
  dataGridView_PaymentTypes.DataSource = query.ToList();
Run Code Online (Sandbox Code Playgroud)

我的课:

private class DataBindingProjection
{
  public Guid PaymentTypeId { get; set; }
  public Guid CorporationId { get; set; }
  public Guid TokenId { get; set; }
  public bool IsActive { get; set; }
  public string Description { get; set; }
  public int CashChargeCodeType { get; set; }
  public int SortOrder { get; set; }
  public int ExcludeCreditCode { get; set; }
  public bool IsUpdated { get; set; }
  public bool IsAdded { get; set; }
  public bool ClearUpdatedAndAdded { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在保存更改的按钮中:

private void button_SaveChanges2_Click(object sender, EventArgs e)
{
  button_SaveChanges2.Enabled = false;
  _entities.SaveChanges();
  timer1.Enabled = true;
  button_SaveChanges2.Enabled = true;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

为了回应bmused:

在班级定义:

private SuburbanPortalEntities _entities;
Run Code Online (Sandbox Code Playgroud)

在我的负载中定义:

  var bs = new BindingSource();
  _entities.PaymentTypes.Where(x => x.CorporationId == _currentcorp.CorporationId).Load;
  bs.DataSource = _entities.PaymentTypes.Local.ToBindingList();
  dataGridView_PaymentTypes.DataSource = bs;
Run Code Online (Sandbox Code Playgroud)

它显示它无法加载符号Load和Local:

在此输入图像描述

小智 14

使用Winforms和Entity Framework进行双向数据绑定可以通过创建一个IBindinglistfrom DbContext Local ObservableCollection<T>并将其设置为DataSourcea来实现BindingSource.例:

private BindingSource bs = new BindingSource();
private MyDbContext context = new MyDbContext();

context.MyEntities.Where(x=>x.SomeProperty == 2).Load(); 
bs.DataSource = context.MyEntities.Local.ToBindingList(); 
myDataGridView.DataSource = bs;
Run Code Online (Sandbox Code Playgroud)