EntityFrameworkCore.Exceptions.PostgreSQL
11.0.0
dotnet add package EntityFrameworkCore.Exceptions.PostgreSQL --version 11.0.0
NuGet\Install-Package EntityFrameworkCore.Exceptions.PostgreSQL -Version 11.0.0
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="11.0.0" />
<PackageVersion Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="11.0.0" />
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" />
paket add EntityFrameworkCore.Exceptions.PostgreSQL --version 11.0.0
#r "nuget: EntityFrameworkCore.Exceptions.PostgreSQL, 11.0.0"
#:package EntityFrameworkCore.Exceptions.PostgreSQL@11.0.0
#addin nuget:?package=EntityFrameworkCore.Exceptions.PostgreSQL&version=11.0.0
#tool nuget:?package=EntityFrameworkCore.Exceptions.PostgreSQL&version=11.0.0
![]()
EntityFramework.Exceptions
Handle database errors easily when working with Entity Framework Core and ADO.NET. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql
Entity Framework Community Standup Live Show
OSS Power-Ups: EntityFramework.Exceptions
Nick Chapsas - The Smart Way to Handle EF Core Exceptions
Sponsors
A huge thanks to everyone (individuals or organisations) who have sponsored EntityFramework.Exceptions, but a massive thanks in particular to
- Entity Framework Extensions and Dapper Plus are major sponsors and are proud to contribute to the development of EntityFramework.Exceptions
- JetBrains is sponsoring the EntityFramework.Exceptions project as part of their OSS Power-Ups series
What does EntityFramework.Exceptions do?
When using Entity Framework Core for data access all database exceptions are wrapped in DbUpdateException. If you need to find
whether the exception was caused by a unique constraint, value being too long or value missing for a required column you need to dig into
the concrete DbException subclass instance and check the error code to determine the exact cause.
EntityFramework.Exceptions simplifies this by handling all the database specific details and throwing different exceptions. All you have
to do is to configure DbContext by calling UseExceptionProcessor and handle the exception(s) you need, such as UniqueConstraintException,
CannotInsertNullException, MaxLengthExceededException, NumericOverflowException, or ReferenceConstraintException.
In case of UniqueConstraintException and ReferenceConstraintException you can get the name of the associated constraint with ConstraintName property. The ConstraintProperties will contain the properties that are part of the constraint.
ConstraintName and ConstraintProperties will be populated only if the index is defined in the Entity Framework Model. These properties will not be populated if the index exists in the database but isn't part of the model or if the index is added with MigrationBuilder.Sql method.
ConstraintName and ConstraintProperties will not be populated when using SQLite.
All these exceptions inherit from DbUpdateException for backwards compatibility.
How do I get started?
First, install the package corresponding to your database:
dotnet add package EntityFrameworkCore.Exceptions.SqlServer
dotnet add package EntityFrameworkCore.Exceptions.MySql
dotnet add package EntityFrameworkCore.Exceptions.PostgreSQL
dotnet add package EntityFrameworkCore.Exceptions.Sqlite
dotnet add package EntityFrameworkCore.Exceptions.Oracle
EntityFrameworkCore.Exceptions.MySql supports the official MySql.EntityFrameworkCore provider. If you use a MySqlConnector-based
provider (Pomelo or its forks such as Microting.EntityFrameworkCore.MySql), see MySqlConnector-based providers.
Next, in your DbContext OnConfiguring method call UseExceptionProcessor extension method:
class DemoContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductSale> ProductSale { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Product>().HasIndex(u => u.Name).IsUnique();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseExceptionProcessor();
}
}
You will now start getting different exception for different database error. For example, when a unique constraints fails you will get UniqueConstraintException exception:
using (var demoContext = new DemoContext())
{
demoContext.Products.Add(new Product
{
Name = "demo",
Price = 10
});
demoContext.Products.Add(new Product
{
Name = "demo",
Price = 100
});
try
{
demoContext.SaveChanges();
}
catch (UniqueConstraintException e)
{
//Handle exception here
Console.WriteLine($"Unique constraint {e.ConstraintName} violated. Duplicate value for {e.ConstraintProperties[0]}");
}
}
If you want to use another native SQLite binary instead of e_sqlite3.dll use the EntityFrameworkCore.Exceptions.Sqlite.Core package. This package depends on Microsoft.Data.Sqlite.Core package, which doesn't include SQLite native binary so you can use any native binary you want.
Usage with DbContext pooling
Instead of calling UseExceptionProcessor in the OnConfiguring method, add it where you add your DbContextPool:
// Replace UseNpgsql with the sql flavor you're using
builder.Services.AddDbContextPool<DemoContext>(options => options
.UseNpgsql(config.GetConnectionString("DemoConnection"))
.UseExceptionProcessor());
Using a provider without a dedicated package
If your database provider has no EntityFrameworkCore.Exceptions.* package (for example, Devart dotConnect providers), implement
IDbExceptionClassifier yourself and pass it to UseExceptionProcessor. This overload lives in the base
EntityFrameworkCore.Exceptions.Common package, so you can reference that package directly:
For example, with Devart dotConnect for PostgreSQL, classify errors by the
SQLSTATE code exposed by PgSqlException.ErrorCode:
using System.Data.Common;
using DbExceptionClassifier.Common;
using Devart.Data.PostgreSql;
using EntityFramework.Exceptions.Common;
public class DevartPostgreSqlExceptionClassifier : IDbExceptionClassifier
{
public bool IsReferenceConstraintError(DbException exception) => exception is PgSqlException { ErrorCode: "23503" or "23001" }; // foreign_key_violation, restrict_violation
public bool IsCannotInsertNullError(DbException exception) => exception is PgSqlException { ErrorCode: "23502" }; // not_null_violation
public bool IsNumericOverflowError(DbException exception) => exception is PgSqlException { ErrorCode: "22003" }; // numeric_value_out_of_range
public bool IsUniqueConstraintError(DbException exception) => exception is PgSqlException { ErrorCode: "23505" }; // unique_violation
public bool IsMaxLengthExceededError(DbException exception) => exception is PgSqlException { ErrorCode: "22001" }; // string_data_right_truncation
public bool IsDeadlockError(DbException exception) => exception is PgSqlException { ErrorCode: "40P01" }; // deadlock_detected
}
optionsBuilder
.UsePostgreSql(connectionString)
.UseExceptionProcessor(new DevartPostgreSqlExceptionClassifier());
The same approach works for any other ADO.NET provider: check the provider's exception type and its error codes.
ConstraintName and ConstraintProperties are populated the same way as for built-in providers: by matching constraint names from the EF Core model against the database error message.
MySqlConnector-based providers
There is no dedicated package for EF Core providers built on MySqlConnector, such as
Pomelo.EntityFrameworkCore.MySql and its forks such as Microting.EntityFrameworkCore.MySql. You don't need to write a classifier for them:
the DbExceptionClassifier.MySqlConnector package already has one for MySqlConnector.
dotnet add package EntityFrameworkCore.Exceptions.Common
dotnet add package DbExceptionClassifier.MySqlConnector
using DbExceptionClassifier.MySqlConnector;
using EntityFramework.Exceptions.Common;
optionsBuilder
.UseMySql(connectionString, serverVersion)
.UseExceptionProcessor(new MySqlConnectorExceptionClassifier());
DbExceptionClassifier
If you don't use Entity Framework Core, you can use the DbExceptionClassifier packages to classify ADO.NET database exceptions directly. They provide a unified IDbExceptionClassifier interface without any EF Core dependency.
dotnet add package DbExceptionClassifier.PostgreSQL
var classifier = new PostgreSQLExceptionClassifier();
try
{
await command.ExecuteNonQueryAsync();
}
catch (DbException ex) when (classifier.IsUniqueConstraintError(ex))
{
// Handle unique constraint violation
}
See the DbExceptionClassifier README for full documentation.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- DbExceptionClassifier.PostgreSQL (>= 1.0.2)
- EntityFrameworkCore.Exceptions.Common (>= 11.0.0)
NuGet packages (16)
Showing the top 5 NuGet packages that depend on EntityFrameworkCore.Exceptions.PostgreSQL:
| Package | Downloads |
|---|---|
|
Newguys.Sport
Package Description |
|
|
AuthPermissions.AspNetCore
Provides extra authorization and multi-tenant features to an ASP.NET Core application. |
|
|
DotNetBrightener.DataAccess.EF.PostgreSQL
Package Description |
|
|
Identity.Module.API
A set of libraries to easily integrate and extend authentication in ASP.NET Core projects, using ASP.NET Core Identity. |
|
|
Newguys.GameProviders.Common
Package Description |
GitHub repositories (6)
Showing the top 6 popular GitHub repositories that depend on EntityFrameworkCore.Exceptions.PostgreSQL:
| Repository | Stars |
|---|---|
|
neozhu/CleanArchitectureWithBlazorServer
This repository is designed to create an enterprise Blazor Server application that follows the principles of Clean Architecture and implements Blazor Clean Architecture best practices for scalability, maintainability, and testability.
|
|
|
JonPSmith/AuthPermissions.AspNetCore
This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
|
|
|
yanpitangui/dotnet-api-boilerplate
A Dotnet 9.0 WebApi template project. MediatR, Swagger, Mapper, Serilog and more implemented.
|
|
|
Valour-Software/Valour
Valour is bringing communities into the future with unique features, blazing performance, and respect for users.
|
|
|
neozhu/cleanaspire
CleanAspire is a cloud-native template built on Aspire, using .NET 10 Minimal APIs and Blazor WebAssembly to deliver lightweight, scalable PWAs with offline support.
|
|
|
baranacikgoz/modular-monolith-ddd-vsa-webapi
A .NET 10 Webapi boilerplate with Modular Monolith approach, Domain-Driven Design and Vertical Slices architecture along with Clean Architecture principles per feature.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.0 | 330 | 11/20/2026 |
| 10.0.2 | 3,215 | 11/16/2026 |
| 10.0.1 | 523,138 | 5/24/2026 |
| 10.0.0 | 247,577 | 3/19/2026 |
| 8.1.3 | 2,802,595 | 8/3/2024 |
| 8.1.2 | 551,765 | 3/22/2024 |
| 8.1.1 | 2,387 | 3/21/2024 |
| 8.1.0 | 17,033 | 3/16/2024 |
| 8.0.0 | 223,518 | 12/11/2023 |
| 6.0.3 | 1,480,200 | 3/16/2022 |
| 3.1.4 | 155,338 | 2/12/2021 |
| 3.1.1 | 68,215 | 1/22/2020 |
| 1.0.1 | 18,606 | 6/11/2019 |
| 1.0.0 | 4,827 | 1/23/2019 |
| 0.1.0-beta | 921 | 12/5/2018 |
- Update to .NET 11 and Entity Framework Core 11
- Add UseExceptionProcessor overload that accepts a custom IDbExceptionClassifier
- Populate ConstraintName and ConstraintProperties for alternate key violations
- Fix constraint details when one constraint name contains another (for example IX_Users and IX_Users_Name): the longest matching name is now used
- EntityFrameworkCore.Exceptions.MySQL.Pomelo is discontinued. With Pomelo or other MySqlConnector-based providers, use UseExceptionProcessor(new MySqlConnectorExceptionClassifier()) from the DbExceptionClassifier.MySqlConnector package