Ho bisogno di un modo per distinguere tra le eccezioni SQL utilizzando il framework di entità LINQ, ad esempio come distinguere la violazione del vincolo della chiave esterna o la violazione del vincolo univoco quando tutto quello che ottengo dalla DbUpdateException è una tonnellata di interni nidificati eccezioni e inutili lunghi messaggi di errore? Ci sono eccezioni di livello inferiore dove posso fare qualcosa come "Catch FKException"; prendere "uniqueException" o qualcosa del genere.Gestire le eccezioni nell'entità framework 4
risposta
Utilizzando i codici di errore sql ...
catch (DbUpdateException ex)
{
var sqlex = ex.InnerException.InnerException as SqlException;
if (sqlex != null)
{
switch (sqlex.Number)
{
case 547: throw new ExNoExisteUsuario("No existe usuario destino."); //FK exception
case 2627:
case 2601:
throw new ExYaExisteConexion("Ya existe la conexion."); //primary key exception
default: throw sqlex; //otra excepcion que no controlo.
}
}
throw ex;
}
try
{
//code
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
string rs = "";
foreach (var eve in e.EntityValidationErrors)
{
rs = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
Console.WriteLine(rs);
foreach (var ve in eve.ValidationErrors)
{
rs += "<br />" + string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
}
}
throw new Exception(rs);
}
Se si desidera essere veramente gentili, l'ultima riga deve essere "lanciare una nuova eccezione (rs, e)" in modo che la persona successiva sulla catena possa controllare l'eccezione interna e vedere lo stacktrace e quant'altro. (Anche lanciare un'eccezione personalizzata invece di un generico 'Exception' sarebbe bello) –
Ma in realtà devo distinguere tra loro, ho bisogno di lanciare diverse eccezioni a seconda del tipo di errore del database. – user1777914
ho scritto metodi di utilità coppia per questo:
public static class DbUtils
{
/// <summary>
/// Takes a code block that updates database, runs it and catches db exceptions. If the caught
/// exception is one of those that are ok to ignore (okToIgnoreChecks) then no
/// exception is raised and result is returned. Otherwise an exception is rethrown.
///
/// This function is intended to be run within an explicit transaction, i.e.:
/// using (var transaction = db.Database.BeginTransaction()), which should be committed/rolledback afterwards.
/// Otherwise, if you don't use a transaction discard the db context or in other words make this operation
/// the only one that you run within implicit transaction.
///
/// This function can wrap a single DB statement, but it's more efficient to wrap multiple statements
/// so that locks are held for shorter period of time.
/// If an exception occurs within a transaction and is caught by this function, all other changes
/// will be still saved to DB on commit if transaction is used.
/// </summary>
/// <typeparam name="T">Any result returned by the code block</typeparam>
/// <param name="context">Database connection</param>
/// <param name="dbCodeBlock">
/// Code block to execute that updates DB. It's expected, but not critical that
/// this code does not throw any other exceptions. Do not call SaveChanges() from the code block itself. Let this
/// function do it for you.
/// </param>
/// <param name="okToIgnoreChecks">
/// List of functions that will check if an exception can be ignored.
/// </param>
/// <returns>Returns number of rows affected in DB and result produced by the code block</returns>
public static Tuple<int, T> IgnoreErrors<T>(DbContext context,
Func<T> dbCodeBlock, params Func<DbUpdateException, bool>[] okToIgnoreChecks)
{
var result = dbCodeBlock();
try
{
var rowsAffected = context.SaveChanges();
return Tuple.Create(rowsAffected, result);
}
catch (DbUpdateException e)
{
if (okToIgnoreChecks.Any(check => check(e)))
return Tuple.Create(0, result);
throw;
}
}
public static bool IsDuplicateInsertError(DbUpdateException e)
{
return GetErrorCode(e) == 2601;
}
public static bool IsForeignKeyError(DbUpdateException e)
{
return GetErrorCode(e) == 547;
}
public static T UpdateEntity<T>(DbContext context, T entity, Action<T> entityModifications)
where T : class
{
return EntityCrud(context, entity, (db, e) =>
{
db.Attach(e);
entityModifications(e);
return e;
});
}
public static T DeleteEntity<T>(DbContext context, T entity)
where T : class
{
return EntityCrud(context, entity, (db, e) => db.Remove(e));
}
public static T InsertEntity<T>(DbContext context, T entity)
where T : class
{
return EntityCrud(context, entity, (db, e) => db.Add(e));
}
public static T EntityCrud<T>(DbContext context, T entity, Func<DbSet<T>, T, T> crudAction)
where T : class
{
return crudAction(context.Set<T>(), entity);
}
}
Ecco come è possibile utilizzarlo. Esempio di inserimento di una riga potenzialmente duplicato:
DbUtils.IgnoreErrors(_db,() => DbUtils.InsertEntity(_db, someEntity),
DbUtils.IsDuplicateInsertError);
verrà generata Nessuna eccezione.
Simile al precedente esempio, ma gestire un'eccezione di violazione di FK esplicitamente:
try
{
var numInserted = DbUtils.IgnoreErrors(_db,() => DbUtils.InsertEntity(_db, someEntity), DbUtils.IsDuplicateInsertError).Item1;
// no FK exception, but maybe unique index violation, safe
// to keep going with transaction
}
catch (DbUpdateException e)
{
if (DbUtils.IsForeignKeyError(e))
{
// you know what to do
}
throw; // rethrow other db errors
}
Alla fine è possibile chiamare il commit della transazione se si dispone di una transazione esplicita, altrimenti salvataggio è stato chiamato dal contesto già.
- 1. Come gestire le eccezioni?
- 2. Come gestire le eccezioni REST?
- 3. Come gestire le eccezioni per Route multipla
- 4. Come gestire le eccezioni generate nei filtri?
- 5. Come gestire le diverse eccezioni nell'attività?
- 6. come gestire le eccezioni in junit
- 7. Dove e come gestire le eccezioni Stripe?
- 8. Gestire le eccezioni globali in VB
- 9. Come gestire le eccezioni all'interno dell'attore?
- 10. Method Fode AsyncDecorator per gestire le eccezioni
- 11. PowerShell 2.0 e come gestire le eccezioni?
- 12. Come gestire le eccezioni in un blocco play framework 2 Async (Scala)
- 13. Entity Framework: come gestire correttamente le eccezioni che si verificano a causa di vincoli SQL
- 14. Come gestire le eccezioni in OnNext quando si utilizza ObserveOn?
- 15. miglior approccio e pratiche per gestire le eccezioni in primavera?
- 16. Come gestire le eccezioni con nunit e moq?
- 17. Modo corretto di gestire le eccezioni in Python?
- 18. Come gestire le eccezioni di aggiornamento di Google Guava Cache?
- 19. Come lanciare e gestire le eccezioni nello Schema R6RS
- 20. È possibile gestire le eccezioni nelle query LINQ?
- 21. come gestire le eccezioni nel codice RESTful basato su JSON?
- 22. Gestire correttamente le eccezioni HttpClient entro async/attendere
- 23. Come gestire correttamente le eccezioni non controllate importanti
- 24. Come devo gestire le eccezioni in questa funzione C#?
- 25. Come dovrei gestire le eccezioni controllate in Spring's JavaConfig?
- 26. Come gestire le eccezioni in Attività con debugger?
- 27. Come gestire le eccezioni da un thread di BackgroundWorker?
- 28. Come gestire le eccezioni WinRT che risultano in Eccezione?
- 29. Gestire con garbo le eccezioni dell'applicazione in un'applicazione Tornado
- 30. Android: come devo gestire le eccezioni di accesso ai DB?
Spesso le eccezioni interne contengono codici di errore numerici, hai provato a cercarli? Pubblica un esempio di eccezione nella tua domanda, assicurati di evidenziare il testo e fai clic sul pulsante '{}' per renderlo ben formattato. –