0

我正在使用 xtragrid 来保存、加载和修改数据。现在切换到 EF5 后,它就不起作用了。尝试将我的代码更改为localtobindinglist,但数据库中没有任何更改。有什么建议么?

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);
            } 
        }

它有点保存:如果我只添加一个条目它不会保存,但如果我添加两个它会保存。这怎么可能呢?

4

2 回答 2

0

您应该将实体添加到您的上下文中,或者如果您正在执行更新,请附加实体并将 State 属性设置为已修改:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=619

于 2013-08-05T19:40:40.317 回答
0

尝试让数据源成为ObservableCollection,而不是 List:

konstruksiyonBindingSource.DataSource = db.Konstruksiyon.Local; 

它知道更改发生的时间,因此应该更适合更改跟踪

于 2013-09-24T09:32:05.867 回答