DbExceptionClassifier.Common 1.0.0
![]()
DbExceptionClassifier
Classify ADO.NET database exceptions by error type. Works with any ADO.NET provider without requiring Entity Framework Core.
Supports PostgreSQL, SQL Server, SQLite, Oracle, MySQL (MySql.Data and MySqlConnector).
What does DbExceptionClassifier do?
When working with ADO.NET, database exceptions are provider-specific. To determine whether an error was caused by a unique constraint violation, a foreign key violation, or a null constraint, you need to inspect the provider-specific exception type and error codes.
DbExceptionClassifier provides a unified IDbExceptionClassifier interface that classifies DbException instances into common error types:
- Unique constraint violation
- Reference (foreign key) constraint violation
- Cannot insert null
- Max length exceeded
- Numeric overflow
- Deadlock
Getting started
Install the package for your database:
dotnet add package DbExceptionClassifier.PostgreSQL
dotnet add package DbExceptionClassifier.SqlServer
dotnet add package DbExceptionClassifier.Sqlite
dotnet add package DbExceptionClassifier.Oracle
dotnet add package DbExceptionClassifier.MySQL
dotnet add package DbExceptionClassifier.MySQL.Pomelo
Usage
Create an instance of the classifier for your database and use it to classify exceptions:
var classifier = new PostgreSQLExceptionClassifier();
try
{
// Execute your ADO.NET command
await command.ExecuteNonQueryAsync();
}
catch (DbException ex) when (classifier.IsUniqueConstraintError(ex))
{
// Handle unique constraint violation
}
catch (DbException ex) when (classifier.IsReferenceConstraintError(ex))
{
// Handle foreign key violation
}
IDbExceptionClassifier interface
public interface IDbExceptionClassifier
{
bool IsUniqueConstraintError(DbException exception);
bool IsReferenceConstraintError(DbException exception);
bool IsCannotInsertNullError(DbException exception);
bool IsMaxLengthExceededError(DbException exception);
bool IsNumericOverflowError(DbException exception);
bool IsDeadlockError(DbException exception);
}
CompositeExceptionClassifier
If your application connects to multiple database types, use CompositeExceptionClassifier to combine multiple classifiers into one. It delegates to each classifier and returns true if any of them matches:
var classifier = new CompositeExceptionClassifier(
new PostgreSQLExceptionClassifier(),
new SqlServerExceptionClassifier()
);
try
{
await command.ExecuteNonQueryAsync();
}
catch (DbException ex) when (classifier.IsUniqueConstraintError(ex))
{
// Works regardless of which database threw the exception
}
Tip
If you want to use another native SQLite binary instead of e_sqlite3.dll use the DbExceptionClassifier.Sqlite.Core package. This package depends on Microsoft.Data.Sqlite.Core, which doesn't include the native SQLite binary so you can use any native binary you want.
Entity Framework Core
If you are using Entity Framework Core, use the EntityFrameworkCore.Exceptions packages instead. They build on top of DbExceptionClassifier and provide typed exceptions that inherit from DbUpdateException.
Showing the top 20 packages that depend on DbExceptionClassifier.Common.
| Packages | Downloads |
|---|---|
|
DbExceptionClassifier.SqlServer
Classify SQL Server database exceptions by error type such as unique constraint violation, foreign key violation, and more
|
1 |
Initial release
.NET 10.0
- No dependencies.
| Version | Downloads | Last updated |
|---|---|---|
| 1.0.0 | 1 | 03/23/2026 |