[英]datagridview binding to entity not updating database
我正在從實體對象填充網格,它正在顯示數據。 當我進行更改並將其保存回來時,沒有任何更新。
這是我的代碼:
在我的加載事件中:
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:
可以通過從DbContext
Local
ObservableCollection<T>
創建IBindinglist
並將其設置為BindingSource
的DataSource
來實現與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;
您將更改實體的預計副本的屬性,而實體本身保持不變。 這就是為什么保存無效 - 實體保持不變。
您需要將實體本身作為DataSource綁定到網格,或者在更新投影實例的屬性時更新相應實體的屬性。
.Load()
和.Local
將是可見的,當使用參考:
using System.Data.Entity;
您正在創建新的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
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();
在此之后,您將能夠將其保存到數據庫中。
看一下我關於綁定datagridView的帖子,該方法非常有效並且非常有用: 將datagridview綁定到數據庫實體的最佳方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.