0

I am using xtragrid to save, load and modify data. Now after switching to EF5, it just doesn't work. Tried changing my code to local and to tobindinglist, but nothing gets changed in the database. Any suggestions?

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

Some updates after changing:

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

It sort of saves: it doesn't save if I add only one entry, yet it saves if I add two. How is that even possible?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
Sin5k4
  • 1,556
  • 7
  • 33
  • 57
  • Have you tried it with just `db.Konstruksiyon` ? – Jay Aug 05 '13 at 19:25
  • yeah,i get an error saying "Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported.",however that was the method i used all the time up until ef5 – Sin5k4 Aug 05 '13 at 19:27
  • A simple google search on your error message gave me [this](http://stackoverflow.com/questions/12938371/data-binding-directly-to-a-store-query-dbset-dbquery-dbsqlquery-is-not-suppo). Have you looked into it? – Jay Aug 05 '13 at 19:31
  • Yeah,thats what i did in the first place,tried binding the local datasource.Yet the problem is the bindlingsource does not persist. – Sin5k4 Aug 05 '13 at 19:39
  • Can you try `db.Konstruksiyon.ToList();` as long as the context stays alive for the time the grid is displayed the entities should have change tracking. – Jay Aug 05 '13 at 19:55

2 Answers2

0

You should add the entity to your context, or in case your're performing a update, attach the entity and set the State property to modified:

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

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

Try letting the datasource be an ObservableCollection, instead of a List:

konstruksiyonBindingSource.DataSource = db.Konstruksiyon.Local; 

It knows when the changes occour, and therefore should be better suited for changetracking

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54