2013-02-19 9 views
6

Vedi Resolving optimistic concurrency exceptions with Reload (database wins):comprensione Entity Framework concorrenza ottimistica (database WINS) modello

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

Perché il metodo SaveChanges() viene chiamato dopo Reload()? Questa chiamata non cambierà mai i dati nel database.

risposta

3

Sono d'accordo non è troppo chiaro. L'intenzione di questo pezzo di codice è nella frase

L'entità viene quindi in genere restituita all'utente in qualche forma e devono cercare di apportare nuovamente le modifiche e di salvare nuovamente.

quindi sarebbe stato meglio se avessero aggiunto un commento:

... 
// User evaluates current values and may make new changes. 
try 
{ 
    context.SaveChanges(); 
} 
... 
Problemi correlati