C# datagridview绑定到未更新数据库的实体
我从一个实体对象填充一个网格,它很好地显示了数据。当我进行更改并将其保存回时,没有任何内容正在更新 这是我的密码: 在我的加载事件中:C# datagridview绑定到未更新数据库的实体,c#,winforms,entity-framework,datagridview,C#,Winforms,Entity Framework,Datagridview,我从一个实体对象填充一个网格,它很好地显示了数据。当我进行更改并将其保存回时,没有任何内容正在更新 这是我的密码: 在我的加载事件中: var query = from c in _entities.PaymentTypes where c.CorporationId == _currentcorp.CorporationId select new DataBindingProjection
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;
}
我做错了什么
对BMS使用的响应:
在类级别定义:
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;
显示无法加载符号加载和本地:
通过从
DbContext
Local
observateCollection
创建IBindinglist
并将其设置为BindingSource
的数据源,可以实现Winforms和Entity Framework的双向数据绑定。例如:
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;
您可以更改实体的投影副本的属性,而实体本身保持不变。这就是为什么保存不起作用-实体保持不变
您需要将实体本身作为数据源绑定到网格,或者在更新投影实例的属性时更新相应实体的属性。您正在创建新的DataBindingProjection(),因此我们假设这是一个由上下文控制的类,对吗
假设这样,我发现代码中缺少的是将DataBindingProjection的新实例传递到DbContext(如果使用4.2+,或者如果使用旧版本,则传递到ObjectContext,我建议迁移到5.0)
在调用SaveChanges()之前,需要将()创建的实体附加到上下文中,我在代码中没有看到这一点
是为数据库创建新记录的方法。如果要更改数据库中的记录,不应使用Linq方法创建新对象,而应调用对象本身,这样它就可以拥有EF代理并由EF的ChangeTracker跟踪
对我来说,似乎你有一个新的类,而不是由EF跟踪
如果您这样做,那么它应该可以工作(我假设一个名为Projection的属性位于您的实体中,仅举一个例子):
如果你没有,那么你应该这样做:
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();
var query=from c in _entities.PaymentTypes
其中c.CorporationId==\u currentcorp.CorporationId
新数据绑定投影
{
PaymentTypeId=c.PaymentTypeId,
CorporationId=c.CorporationId,
TokenId=c.TokenId,
IsActive=c.IsActive,
描述=c.描述,
CashChargeCodeType=c.CashChargeCodeType,
SortOrder=c.SortOrder,
ExcludeCreditCode=c.ExcludeCreditCodes,
IsUpdated=c.IsUpdated,
IsAdded=c.IsAdded,
ClearUpdateAndAdded=c.ClearUpdateAndAdded
};
foreach(查询中的var项)
(DbContext)YourInstanceOfContext.Set().Add(项);
dataGridView_PaymentTypes.DataSource=query.ToList();
之后,您将能够将其保存到数据库。.Load()
和.Local
将在使用引用时可见:
using System.Data.Entity;
请看我关于绑定datagridView的帖子,该方法工作得非常好,非常有用:为什么要投影到与实体具有完全相同属性的另一种类型?在测试中,我尝试了几种不同的想法,最后得出了这个结论。虽然不需要,但我把它留下了。不应该是Load
吗Load()
?是的。。。但是无论有没有它们,它都找不到负载,所以()如果找不到也没关系。对不起,我没有早点回来,我病了。无论如何,它表示无法解析符号加载或本地。我将把我的代码贴在最上面供您审阅。@ErocM Entity Framework的哪个版本?上述内容适用于EF 4.1+,因为它使用的是DbContext
API,而不是旧的ObjectContext
API。还要检查您是否引用了System.Data.Entity
.v4.0.30319从何处以及如何获取4.1+?我使用的是.net 4.0 full。@ErocM您可以以nuget软件包的形式获取最新版本,ok获得ef 5.0,并且能够加载扩展名,但找不到本地扩展名。有什么建议吗?
using System.Data.Entity;