EFExceptions.Generator
0.5.0-alpha
dotnet add package EFExceptions.Generator --version 0.5.0-alpha
NuGet\Install-Package EFExceptions.Generator -Version 0.5.0-alpha
<PackageReference Include="EFExceptions.Generator" Version="0.5.0-alpha"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="EFExceptions.Generator" Version="0.5.0-alpha" />
<PackageReference Include="EFExceptions.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add EFExceptions.Generator --version 0.5.0-alpha
#r "nuget: EFExceptions.Generator, 0.5.0-alpha"
#:package EFExceptions.Generator@0.5.0-alpha
#addin nuget:?package=EFExceptions.Generator&version=0.5.0-alpha&prerelease
#tool nuget:?package=EFExceptions.Generator&version=0.5.0-alpha&prerelease
EFExceptions.Generator
Friendly Exceptions for DbContext's
After installing the package there are three Attributes that can be placed on DbContext derived classes.
Attribute | Target |
---|---|
[SqlExceptions] |
SqlServer DbContext |
[SQLiteExceptions] |
SQLite DbContext |
[MySqlExceptions] |
MySql DbContext |
Attribute | Dependency |
---|---|
SqlExceptions | Microsoft.EntityFrameworkCore.SqlServer |
SQLiteExceptions | Microsoft.EntityFrameworkCore.Sqlite |
MySqlExceptions | MySql.EntityFrameworkCore |
It is expected these dependencies be installed if the application has a DbContext configured for the intended attribute.
The DbContext
has to marked partial for the generator to extent the class.
The generator overrides all DbContext.SaveChanges[Async]
methods capturing any errors and converts them to a meaningful exception.
Example:
[SqlExceptions]
public partial class SqlContext(DbContextOptions<SqlContext> options) :
DbContext(options), IStudentContext<SqlContext>
{
public DbSet<Student> Students { get; set; }
}
Note: Only the dependency for the DbContext required need be installed.
Before
Duplicate Unique Index Item Test:
SqlContext
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
SQLLiteContext
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
DbUpdateException An error occurred while saving the entity changes. See the inner exception for details.
MySqlContext
DbUpdateException Could not save changes. Please configure your entity type accordingly.
DbUpdateException Could not save changes. Please configure your entity type accordingly.
DbUpdateException Could not save changes. Please configure your entity type accordingly.
After
Duplicate Unique Index Item Test:
SqlContext
DuplicateKeyException Violation of PRIMARY KEY constraint 'PK_Students'. Cannot insert duplicate key in object 'dbo.Students'. The duplicate key value is (01).
DuplicateKeyWithUniqueIndexException Cannot insert duplicate key row in object 'dbo.Students' with unique index 'IX_Students_Name'. The duplicate key value is (Foo).
ForeignKeyConstraintConflictException The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Students_Categories_CategoryId". The conflict occurred in database "TestAppData", table "dbo.Categories", column 'Id'.
SQLLiteContext
DuplicateKeyException SQLite Error 19: 'UNIQUE constraint failed: Students.Id'.
DuplicateKeyWithUniqueIndexException SQLite Error 19: 'UNIQUE constraint failed: Students.Name'.
ForeignKeyConstraintConflictException SQLite Error 19: 'FOREIGN KEY constraint failed'.
MySqlContext
DuplicateKeyException Duplicate entry '01' for key 'students.PRIMARY'
DuplicateKeyWithUniqueIndexException Duplicate entry 'Foo' for key 'students.IX_Students_Name'
ForeignKeyConstraintConflictException Cannot add or update a child row: a foreign key constraint fails (`testappdata`.`students`, CONSTRAINT `FK_Students_Categories_CategoryId` FOREIGN KEY (`CategoryId`) REFERENCES `categories` (`Id`) ON DELETE CASCADE)
Copyright 2024, Brian Parker
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.5.0-alpha | 93 | 1/31/2024 |
Initial release for evaluation