EntityFrameworkCore.Exceptions.Common
8.1.3
dotnet add package EntityFrameworkCore.Exceptions.Common --version 8.1.3
NuGet\Install-Package EntityFrameworkCore.Exceptions.Common -Version 8.1.3
<PackageReference Include="EntityFrameworkCore.Exceptions.Common" Version="8.1.3" />
paket add EntityFrameworkCore.Exceptions.Common --version 8.1.3
#r "nuget: EntityFrameworkCore.Exceptions.Common, 8.1.3"
// Install EntityFrameworkCore.Exceptions.Common as a Cake Addin #addin nuget:?package=EntityFrameworkCore.Exceptions.Common&version=8.1.3 // Install EntityFrameworkCore.Exceptions.Common as a Cake Tool #tool nuget:?package=EntityFrameworkCore.Exceptions.Common&version=8.1.3
EntityFramework.Exceptions
Handle database errors easily when working with Entity Framework Core. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql
Entity Framework Community Standup Live Show
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.
[!WARNING]
ConstraintName
andConstraintProperties
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.
[!WARNING]
ConstraintName
andConstraintProperties
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.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]}");
}
}
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());
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.14)
-
net8.0
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.0)
NuGet packages (11)
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.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.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
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 |
GitHub repositories (1)
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 |
---|---|---|
8.1.3 | 243,207 | 8/3/2024 |
8.1.2 | 484,242 | 3/22/2024 |
8.1.1 | 3,185 | 3/21/2024 |
8.1.0 | 26,597 | 3/16/2024 |
8.0.0.2 | 131,712 | 1/18/2024 |
8.0.0 | 194,700 | 12/11/2023 |
6.0.3 | 2,244,812 | 3/16/2022 |
3.1.4 | 726,323 | 2/12/2021 |
3.1.1 | 157,906 | 1/22/2020 |
1.0.1 | 53,093 | 6/11/2019 |
1.0.0 | 4,967 | 1/23/2019 |
0.1.0-beta | 1,907 | 12/5/2018 |
Add ConstraintName, ConstraintProperties and SchemaQualifiedTableName to UniqueConstraintException and ReferenceConstraintException