2

Hi i am facing a problem in my code.

public static void settingSoundxStringLawyers_info() 
        {

            mydatabaseEntities db2 = new mydatabaseEntities();

            var UniqueNameObjects = from a in db2.Lawyers_info 
                                   group a by  a.Name into g
                                   where g.Count()==1
                                    select new Names { Name = g.Key };

            List<Names> list = UniqueNameObjects.ToList();
            for (int i=0; i<150 ; i++)
            {
                string name = list.ElementAt(i).Name;
                Lawyers_info obj = db2.Lawyers_info.Where(l => l.Name == name).First();


                string scode = soundxmethod(obj.Name);
                obj.SoundxString = scode;
                db2.Entry(obj).State = EntityState.Modified;
                //var dbEntityEntry = db2.Entry(obj);
                //dbEntityEntry.Property(s=>s.SoundxString).IsModified = true;

                Console.WriteLine("Index: ----> "+(i+1));
                //db2.Lawyers_info.Add(obj);

                //db2.Set(Lawyers_info).SqlQuery("update Lawyers_info values(SoundxString = @p)",new sq)
                db2.SaveChanges();
            }
            db2.SaveChanges();
        }

i tried each commented lines but not woking. the changes are not saved in db.

Mate
  • 4,976
  • 2
  • 32
  • 38
wasipeer
  • 1,015
  • 11
  • 23

1 Answers1

0

Since you are getting the "Lawyers_info" Entity from the database, it will have a state of Modified after you changed a property (no need to set EntityState.Modified).

Plus you only need to call SaveChanges() once. Either right after you modify the entity, OR after you've modified all 150. But not both.

Also make sure to get the rows affected on SaveChanges (it returns an int). That way you can tell who many rows were udpated. HTH

Carl Prothman
  • 1,461
  • 13
  • 23