我正在从实体对象填充网格,它正在显示数据。当我进行更改并将其保存回来时,没有任何更新。
这是我的代码:
在我的加载事件中:
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();
我的课程:
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; }
}
在保存更改的按钮中:
private void button_SaveChanges2_Click(object sender, EventArgs e)
{
button_SaveChanges2.Enabled = false;
_entities.SaveChanges();
timer1.Enabled = true;
button_SaveChanges2.Enabled = true;
}
我做错了什么?
回应bmused:
在班级定义:
private SuburbanPortalEntities _entities;
在我的负载中定义:
var bs = new BindingSource();
_entities.PaymentTypes.Where(x => x.CorporationId == _currentcorp.CorporationId).Load;
bs.DataSource = _entities.PaymentTypes.Local.ToBindingList();
dataGridView_PaymentTypes.DataSource = bs;
显示它无法加载符号Load和Local:
答案 0 :(得分:13)
通过从IBindinglist
DbContext
Local
创建ObservableCollection<T>
并将其设置为DataSource
,可以实现与Winforms和Entity Framework的双向数据绑定一个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;
答案 1 :(得分:4)
您正在更改实体的预计副本的属性,而实体本身保持不变。这就是为什么保存不起作用的原因 - 实体保持不变。
您需要将实体本身作为DataSource绑定到网格,或者在更新投影实例的属性时更新相应实体的属性。
答案 2 :(得分:2)
.Load()
和.Local
将会显示:
using System.Data.Entity;
答案 3 :(得分:1)
您正在创建新的DataBindingProjection(),因此我们假设这是一个由您的上下文控制的类吗?
假设,我看到你的代码中缺少的是你将新的DataBindingProjection实例传递给你的DbContext(如果使用4.2+或者如果使用旧版本则传递给ObjectContext,我建议迁移到5.0)< / p>
在调用SaveChanges()之前,您需要将创建的实体附加()到上下文中,我在您的代码中看不到这一点。
这是您为数据库创建新记录的方法。如果要更改数据库中的记录,则不应使用创建新对象的Linq方法,应该调用对象本身,因此它可以具有EF代理并由EF的ChangeTracker跟踪。 / p>
对我而言,似乎你有一个新的课程没有被EF跟踪.....
如果你做了类似这样的事情,那么它应该有用(我假设一个名为Projection的属性在你的实体中,只是为了一个例子):
var query = from c in _entities.PaymentTypes
where c.CorporationId == _currentcorp.CorporationId
select c.Projection;
dataGridView_PaymentTypes.DataSource = query.ToList();
如果你没有,那么你应该这样做:
var query = from c in _entities.PaymentTypes
where c.CorporationId == _currentcorp.CorporationId
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
};
foreach(var item in query)
(DbContext)YourInstanceOfContext.Set<DataBindingProjection>().Add(item);
dataGridView_PaymentTypes.DataSource = query.ToList();
在此之后,您将能够将其保存到数据库中。
答案 4 :(得分:0)
看一下我关于绑定datagridView的帖子,该方法非常有效且非常有用:Best approach to bind a datagridview to database entity/ies