EFExceptions.Generator 0.5.0-alpha

This is a prerelease version of EFExceptions.Generator.
dotnet add package EFExceptions.Generator --version 0.5.0-alpha
                    
NuGet\Install-Package EFExceptions.Generator -Version 0.5.0-alpha
                    
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="EFExceptions.Generator" Version="0.5.0-alpha">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EFExceptions.Generator" Version="0.5.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="EFExceptions.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 EFExceptions.Generator --version 0.5.0-alpha
                    
#r "nuget: EFExceptions.Generator, 0.5.0-alpha"
                    
#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 EFExceptions.Generator@0.5.0-alpha
                    
#: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=EFExceptions.Generator&version=0.5.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=EFExceptions.Generator&version=0.5.0-alpha&prerelease
                    
Install as a Cake Tool

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

There are no supported framework assets in this package.

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