This file provides guidance to AI coding agents when working with code in this repository.
# Build entire solution
dotnet build EntityFramework.Exceptions.slnx
# Run all tests (requires Docker for Testcontainers)
dotnet test --project EntityFramework.Exceptions/Tests/Tests.csproj
# Run a specific test class
dotnet test --project EntityFramework.Exceptions/Tests/Tests.csproj --filter-class EntityFramework.Exceptions.Tests.PostgreSQLTests
# Run a specific test by name
dotnet test --project EntityFramework.Exceptions/Tests/Tests.csproj --filter-method "*.UniqueColumnViolationThrowsUniqueConstraintException"
# Run tests with coverage (CI command)
dotnet test --project EntityFramework.Exceptions/Tests/Tests.csproj --coverlet --coverlet-output-format lcov --coverlet-include "[EntityFrameworkCore.Exceptions.*]*" --coverlet-include "[DbExceptionClassifier.*]*" --coverlet-exclude "[*]EntityFramework.Exceptions.Common.*Exception"global.json runs dotnet test on Microsoft.Testing.Platform: select the project with --project and filter with the xUnit v3 options (--filter-class, --filter-method, --filter-not-class), not the VSTest --filter expression.
Tests use Testcontainers for PostgreSQL, SQL Server, MySQL, and Oracle (Docker required). SQLite tests use an in-memory shared database and don't need Docker.
The library catches EF Core DbUpdateException and rethrows typed exceptions (UniqueConstraintException, ReferenceConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException). All inherit from DbUpdateException.
Layer 1 — DbExceptionClassifier/: Pure database error classification with no EF Core dependency, shipped as separate packages (see DbExceptionClassifier/README.md). Each provider implements IDbExceptionClassifier (in Common/) to identify error types from the raw DbException (e.g., SQL error codes, PostgreSQL SqlState values). CompositeExceptionClassifier combines several classifiers (a match in any one wins).
The classifier interface also exposes IsDeadlockError, but the EF Core layer has no matching exception: DeadlockException was removed, so deadlocks are not translated by the interceptors. Adding a method to IDbExceptionClassifier means updating every provider classifier, CompositeExceptionClassifier, and the test stub in Tests/CompositeExceptionClassifierTests.cs.
Layer 2 — EntityFramework.Exceptions/: EF Core integration. A generic base interceptor ExceptionProcessorInterceptor<TProviderException> (in Common/) implements ISaveChangesInterceptor and IDbCommandInterceptor. It uses the classifier to detect error types, creates the typed exception via ExceptionFactory, then populates constraint metadata (ConstraintName, ConstraintProperties, SchemaQualifiedTableName) by matching the database exception message against the EF Core model's indexes and keys.
Each provider project is minimal — just a concrete interceptor subclass injecting its classifier, plus a UseExceptionProcessor() extension method on DbContextOptionsBuilder.
SetConstraintDetails in the base interceptor lazily builds a cached list of IndexDetails from:
- Unique indexes:
GetDeclaredIndexes().Where(index => index.IsUnique)→GetMappedTableIndexes() - Primary keys:
FindPrimaryKey()→GetName()
It then matches the constraint name found in the provider exception message against this list. ConstraintProperties is only populated when a match is found. SQLite never populates these properties.
The MySQL classifier has two variants: MySql.Data (DbExceptionClassifier/MySQL) and MySqlConnector (DbExceptionClassifier/MySqlConnector, published as DbExceptionClassifier.MySqlConnector; named DbExceptionClassifier.MySQL.Pomelo before the .NET 11 release). The MySqlConnector project links MySQL/MySQLExceptionClassifier.cs via <Compile Include> and adds the MYSQLCONNECTOR define, which selects the #if MYSQLCONNECTOR branches.
The EF Core layer follows the same pattern: EntityFramework.Exceptions/MySQL (official MySql.EntityFrameworkCore provider, MySql.Data) and EntityFramework.Exceptions/MySqlConnector (published as EntityFrameworkCore.Exceptions.MySqlConnector, for MySqlConnector-based providers such as Pomelo and its maintained fork Microting.EntityFrameworkCore.MySql). The MySqlConnector project links MySQL/MySqlExceptionProcessorInterceptor.cs and defines MYSQLCONNECTOR. It replaces EntityFrameworkCore.Exceptions.MySQL.Pomelo, which was dropped before 11.0.0 and deprecated on nuget.org. Tests/MySqlConnectorTests.cs runs the shared suite with it on Microting. Shared tests detect MySQL with IsAnyMySql() (by provider name), because both providers define an IsMySql() extension and calling it is ambiguous.
Don't merge the MySQL variants into one project switched by an MSBuild property (as Sqlite does with BuildType): the test project needs to reference both variants at once, and NuGet restore can't handle one project with two different classifier references.
- NuGet packages:
EntityFrameworkCore.Exceptions.{Provider}andDbExceptionClassifier.{Provider} - Assembly names follow the same pattern (configured via
Directory.Build.propsusing$(MSBuildProjectName)) - Interceptors:
{Provider}ExceptionProcessorInterceptor - Classifiers:
{Provider}ExceptionClassifier - Test classes:
{Database}Testsextending abstractDatabaseTests - Test fixtures:
{Database}DemoContextFixtureimplementingIAsyncLifetime
Root Directory.Build.props sets the net10.0 target, shared package metadata, and a ProjectReference to ..\Common\Common.csproj for every non-Common project. EntityFramework.Exceptions/Directory.Build.props overrides the target with net11.0, which EF Core 11 requires; DbExceptionClassifier/ stays on net10.0. Each layer folder has its own Directory.Build.props that configures assembly/package naming and the version: the two layers are versioned independently (EntityFramework.Exceptions/ is 11.0.x, tracking EF Core; DbExceptionClassifier/ is 1.0.x). Bump Version, FileVersion, AssemblyVersion, and PackageReleaseNotes together when releasing. Solution file is EntityFramework.Exceptions.slnx (modern XML format).