![]() |
VOOZH | about |
dotnet add package EntityFrameworkCore.Exceptions.Common --version 10.0.1
NuGet\Install-Package EntityFrameworkCore.Exceptions.Common -Version 10.0.1
<PackageReference Include="EntityFrameworkCore.Exceptions.Common" Version="10.0.1" />
<PackageVersion Include="EntityFrameworkCore.Exceptions.Common" Version="10.0.1" />Directory.Packages.props
<PackageReference Include="EntityFrameworkCore.Exceptions.Common" />Project file
paket add EntityFrameworkCore.Exceptions.Common --version 10.0.1
#r "nuget: EntityFrameworkCore.Exceptions.Common, 10.0.1"
#:package EntityFrameworkCore.Exceptions.Common@10.0.1
#addin nuget:?package=EntityFrameworkCore.Exceptions.Common&version=10.0.1Install as a Cake Addin
#tool nuget:?package=EntityFrameworkCore.Exceptions.Common&version=10.0.1Install as a Cake Tool
Handle database errors easily when working with Entity Framework Core and ADO.NET. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql
👁 Target
👁 GitHub Actions Workflow Status
👁 Coverage Status
👁 Ko-Fi
👁 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
A huge thanks to everyone (individuals or organisations) who have sponsored EntityFramework.Exceptions, but a massive thanks in particular to
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.
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.MySql.Pomelo
dotnet add package EntityFrameworkCore.Exceptions.PostgreSQL
dotnet add package EntityFrameworkCore.Exceptions.Sqlite
dotnet add package EntityFrameworkCore.Exceptions.Oracle
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.
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());
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 for full documentation.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 5 NuGet packages that depend on EntityFrameworkCore.Exceptions.Common:
| Package | Downloads |
|---|---|
|
EntityFrameworkCore.Exceptions.SqlServer
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException |
|
|
EntityFrameworkCore.Exceptions.PostgreSQL
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException |
|
|
EntityFrameworkCore.Exceptions.Sqlite
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException. |
|
|
EntityFrameworkCore.Exceptions.MySQL.Pomelo
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException Use this package if you use Pomelo.EntityFrameworkCore.MySql Entity Framework Core provider for MySQL |
|
|
EntityFrameworkCore.Exceptions.MySQL
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException |
Showing the top 1 popular GitHub repositories that depend on EntityFrameworkCore.Exceptions.Common:
| Repository | Stars |
|---|---|
|
JasonBock/Rocks
A mocking library based on the Compiler APIs (Roslyn + Mocks)
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.1 | 33,586 | 5/24/2026 |
| 10.0.0 | 294,361 | 3/19/2026 |
| 8.1.3 | 4,777,339 | 8/3/2024 |
| 8.1.2 | 969,351 | 3/22/2024 |
| 8.1.1 | 4,188 | 3/21/2024 |
| 8.1.0 | 36,685 | 3/16/2024 |
| 8.0.0.2 | 220,302 | 1/18/2024 |
| 8.0.0 | 314,679 | 12/11/2023 |
| 6.0.3 | 3,007,243 | 3/16/2022 |
| 3.1.4 | 896,095 | 2/12/2021 |
| 3.1.1 | 168,106 | 1/22/2020 |
| 1.0.1 | 54,549 | 6/11/2019 |
| 1.0.0 | 9,440 | 1/23/2019 |
| 0.1.0-beta | 2,315 | 12/5/2018 |
- Update to .NET 10 and Entity Framework Core 10
- Set InnerException when throwing custom exceptions