您好我的代码中遇到了问题。
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();
}
我尝试了每个评论的行,但没有尝试。更改不会保存在db。
中答案 0 :(得分:0)
由于您从数据库获取“Lawyers_info”实体,因此在更改属性后(无需设置EntityState.Modified),它将具有Modified状态。
另外,您只需要调用一次SaveChanges()。在您修改实体之后,或者在您修改了所有150之后,或者在修改了所有实体之后。或者不是两者。
还要确保在SaveChanges上获取受影响的行(它返回一个int)。通过这种方式,您可以分辨出哪些行已被更新。 HTH