DbExceptionClassifier.PostgreSQL 1.0.0

dotnet add package DbExceptionClassifier.PostgreSQL --version 1.0.0
                    
NuGet\Install-Package DbExceptionClassifier.PostgreSQL -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DbExceptionClassifier.PostgreSQL" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DbExceptionClassifier.PostgreSQL" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DbExceptionClassifier.PostgreSQL" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DbExceptionClassifier.PostgreSQL --version 1.0.0
                    
#r "nuget: DbExceptionClassifier.PostgreSQL, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DbExceptionClassifier.PostgreSQL@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DbExceptionClassifier.PostgreSQL&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DbExceptionClassifier.PostgreSQL&version=1.0.0
                    
Install as a Cake Tool

EntityFramework.Exceptions

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).

License Target

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

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
}

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.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DbExceptionClassifier.PostgreSQL:

Package Downloads
EntityFrameworkCore.Exceptions.PostgreSQL

Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException, ReferenceConstraintException or DeadlockException instead of generic DbUpdateException

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 416 3/19/2026

Initial release