gpt4 book ai didi

c# - SaveChange 不起作用后刷新数据源

转载 作者:太空宇宙 更新时间:2023-11-03 14:16:18 36 4
gpt4 key购买 nike

我在 ObjectSet 中添加数据并在 ObjectContext 上执行了 SaveChanges。但是新数据没有显示在 DataGrid 中!

代码:

bsWork.DataSource = Program.EC.Addresses;
...
Address newAdr = new Address();
//change properties newAdr
...
Program.EC.Addresses.AddObject(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
如何刷新数据或查看新数据?

UPD:解决方案


...
bsWork.Add(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
...

最佳答案

希望这段代码对您有所帮助。

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);<p></p>

<pre><code>// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
</code></pre>

<p>}</p>

关于c# - SaveChange 不起作用后刷新数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6515222/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com