Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am new to sqlite entity framework 6.
I am trying to update a record in table but it not saving the changes in the database.
I am using the following code to Update Record.
C#
public int saveBill(general_bills cat)
        {

            using (alldbEntities db = new alldbEntities())
            {
                if (cat.ID > 0)
                {
                    //db.Attach(cat);
                    //db.ApplyPropertyChanges("general_bills", cat);
                    //db.Attach(cat);

                    //db.AddTogeneral_bills(cat);
                    //db.Attach(cat);
                    //db.AcceptAllChanges();
                    db.AttachTo("general_bills", cat);
                    //db.Entry(cat).State = EntityState.Modified;
                    db.SaveChanges(true);
                }
                else
                {
                    db.AddTogeneral_bills(cat);
                    db.SaveChanges(true);

                }
                return (int)cat.ID;
            }
        }
Posted

1 solution

Please try is as below.

Note : I hope your cat entity collection is "AddTogeneral_bills"

C#
public int saveBill(general_bills cat)
        {

            using (alldbEntities db = new alldbEntities())
            {
                if (cat.ID > 0)
                {

            var catObj = (from c in db.AddTogeneral_bills
                                    where c.ID=cat.ID
                                    select c).FirstOrDefault();


                   catObj.pro1= cat.pro1;
                   catObj.pro2= cat.pro2;
                   //put your properties

                   db.SaveChanges();
                }
                else
                {
                    db.AddTogeneral_bills.Add(cat);
                    db.SaveChanges();

                }
                return (int)cat.ID;
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900