Domanda

See Resolving optimistic concurrency exceptions with Reload (database wins) :

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);
}

Why the method SaveChanges() is called after Reload()? This call will never change the data in the database.

È stato utile?

Soluzione

I agree it's not too clear. The intention of this piece of code is in the sentence

The entity is then typically given back to the user in some form and they must try to make their changes again and re-save.

So it would have been better if they had added a comment:

...
// User evaluates current values and may make new changes.
try
{
    context.SaveChanges();
}
...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top