Having trouble saving changes in Dataset to SQL LocalDB

485 views Asked by At

My problem is that I can connect to database and in my application everything works great and when I update a field, the changes are shown in the application but when I close and reopen the application no changes where saved, my codes are like below.

ps: my database is inside my application.

var res = db.bibleContext.Where(x => x.Book == bookF && x.Chapter == chapterF && x.Verse == verseF).FirstOrDefault();
            res.favoriteverse = i;
            db.Entry(res).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

and my connection string is:

<add name="Dholybible" connectionString="Data Source=(Localdb)\mssqllocaldb;AttachDbFilename=|DataDirectory|\HollyBible.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
1

There are 1 answers

0
user3785359 On

Here is the Solution. Before save changes, attache the local change sets to db...

var res = db.bibleContext.Where(x => x.Book == bookF && x.Chapter == chapterF && x.Verse == verseF).FirstOrDefault();
        res.favoriteverse = i;
        db.bibleContext.Attach(res);
        db.Entry(res).State = System.Data.Entity.EntityState.Modified;
        db.SaveChanges();