2013-05-17 8 views
8

ottengo questo errore:La convalida non è riuscita per una o più entità. Vedi la proprietà 'EntityValidationErrors' per maggiori dettagli. Primo codice

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. 

quando cerco di aggiornare il database con il comando Update-Database nella Console Package Manager.

Come posso scrivere le linee nella finestra di output in Visual Studio?

ho provato:

try 
{ 
    context.SaveChanges(); 
} 
catch (System.Data.Entity.Validation.DbEntityValidationException e) 
{ 
    foreach (var eve in e.EntityValidationErrors) 
    { 
     System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
      eve.Entry.Entity.GetType().Name, eve.Entry.State); 
     foreach (var ve in eve.ValidationErrors) 
     { 
      System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
       ve.PropertyName, ve.ErrorMessage); 
     } 
    } 
    throw; 
} 

Ma che non ha funzionato. Qualche altro suggerimento su come eseguire il debug di questo?

risposta

20

Non so perché scrivere nella finestra di output VS non funziona e come farlo funzionare. Ma come ultima risorsa, basta scrivere gli errori in un file di testo che dovrebbe funzionare indipendentemente dal tipo di applicazione si hanno:

try 
{ 
    context.SaveChanges(); 
} 
catch (System.Data.Entity.Validation.DbEntityValidationException e) 
{ 
    var outputLines = new List<string>(); 
    foreach (var eve in e.EntityValidationErrors) 
    { 
     outputLines.Add(string.Format(
      "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", 
      DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State)); 
     foreach (var ve in eve.ValidationErrors) 
     { 
      outputLines.Add(string.Format(
       "- Property: \"{0}\", Error: \"{1}\"", 
       ve.PropertyName, ve.ErrorMessage)); 
     } 
    } 
    //Write to file 
    System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines); 
    throw; 

    // Showing it on screen 
    throw new Exception(string.Join(",", outputLines.ToArray())); 

} 
+0

Ciao, wow, perché non ci ho pensato !? Grazie! – Yustme

+2

@Slauma: Grazie .. Bel trucco .. Non ci ho mai pensato .. Questo è il punto in cui la differenza tra i bravi sviluppatori e quelli bravi! ;) – user2394196

4

si può passare lo stack eccezione come qui di seguito.

try 
{ 
    _dbContext.SaveChanges(); 
} 
catch (DbEntityValidationException dbValEx) 
{ 
    var outputLines = new StringBuilder(); 
    foreach (var eve in dbValEx.EntityValidationErrors) 
    { 
    outputLines.AppendFormat("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:" 
     ,DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State); 

    foreach (var ve in eve.ValidationErrors) 
    { 
     outputLines.AppendFormat("- Property: \"{0}\", Error: \"{1}\"" 
     ,ve.PropertyName, ve.ErrorMessage); 
    } 
    } 

throw new DbEntityValidationException(string.Format("Validation errors\r\n{0}" 
    ,outputLines.ToString()), dbValEx); 
} 
+0

Grazie, darò una prova anche nel mio prossimo progetto! +1 – Yustme