使用实体框架5.0和xtragrid保存数据
本文关键字:xtragrid 保存 数据 实体 框架 | 更新日期: 2023-09-27 18:26:27
我正在使用xtragrid来保存、加载和修改数据。现在切换到EF5后,它就不起作用了。尝试将我的代码更改为local
和tobindinglist
,但数据库中没有任何更改。有什么建议吗?
public partial class formKonstruksiyon : Form
{
public Tank_Analizor_DBEntities db;
public formKonstruksiyon()
{
InitializeComponent();
db = new Tank_Analizor_DBEntities();
}
private void formKonstruksiyon_Load(object sender, EventArgs e)
{
konstruksiyonBindingSource.DataSource = db.Konstruksiyon.Local.ToBindingList();
gridControl1.DataSource = konstruksiyonBindingSource.DataSource;
}
private void barButtonItemSave_ItemClick(object sender,
DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
db.SaveChanges();
LibStatic.ShowSuccessMessage();
}
catch (Exception ex)
{
LibStatic.ShowErrorMessage(ex);
}
}
private void silToolStripMenuItem_Click(object sender, EventArgs e)
{
gridView1.DeleteRow(gridView1.FocusedRowHandle);
}
}
更改后的一些更新:
konstruksiyonBindingSource.DataSource = db.Konstruksiyon.ToList();
private void barButtonItemSave_ItemClick(object sender,
DevExpress.XtraBars.ItemClickEventArgs e)
{
LibStatic.GridPostPendingRow(gridView1);
try
{
konstruksiyonBindingSource.DataSource = db.Konstruksiyon.Local.ToBindingList();
db.SaveChanges();
LibStatic.ShowSuccessMessage();
}
catch (Exception ex)
{
LibStatic.ShowErrorMessage(ex);
}
}
它有点保存:如果我只添加一个条目,它不会保存,但如果我添加两个条目,则会保存。这怎么可能呢?
您应该将实体添加到上下文中,或者在执行更新的情况下,附加实体并将State属性设置为modified:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=619
试着让数据源是ObservableCollection,而不是List:
konstruksiyonBindingSource.DataSource = db.Konstruksiyon.Local;
它知道何时发生更改,因此应该更适合于更改跟踪