Xem Resolving optimistic concurrency exceptions with Reload (database wins):Hiểu Entity Framework lạc quan đồng thời (cơ sở dữ liệu thắng) mẫu
using (var context = new BloggingContext())
{
var blog = context.Blogs.Find(1);
blog.Name = "The New ADO.NET Blog";
bool saveFailed;
do
{
saveFailed = false;
try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Update the values of the entity that failed to save from the store
ex.Entries.Single().Reload();
}
} while (saveFailed);
}
Tại sao phương pháp SaveChanges()
được gọi sau khi Reload()
? Cuộc gọi này sẽ không bao giờ thay đổi dữ liệu trong cơ sở dữ liệu.