我在ObjectSet
中添加了数据,并在SaveChanges
上执行了ObjectContext
。但新数据未显示在DataGrid
!
代码:
bsWork.DataSource = Program.EC.Addresses;
...
Address newAdr = new Address();
//change properties newAdr
...
Program.EC.Addresses.AddObject(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
如何刷新数据或查看新数据?
...
bsWork.Add(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
...
答案 0 :(得分:1)
我希望这段代码对您有所帮助。
try {
// Try to save changes, which may cause a conflict.
int num = context.SaveChanges();
Console.WriteLine("No conflicts. " + num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException) {
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders);
// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}