Class DbExceptionClassifier
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
Classifies database exceptions across providers (SQL Server, PostgreSQL, SQLite, MySQL/MariaDB). Used internally by both SaveChangesResultAsync(DbContext, CancellationToken) and SaveChangesResultAsync(DbContext, bool, CancellationToken) overloads. Also available for direct use in repositories that need custom error messages per exception type.
public static class DbExceptionClassifier
- Inheritance
-
DbExceptionClassifier
- Inherited Members
Methods
ExtractConstraintDetail(DbUpdateException)
Attempts to extract a human-readable constraint detail from the exception.
Returns the constraint name or violated column if available, otherwise null.
public static string? ExtractConstraintDetail(DbUpdateException ex)
Parameters
exDbUpdateExceptionThe DbUpdateException to extract details from.
Returns
- string
A constraint detail string, or
nullif no detail can be extracted.
ExtractConstraintIdentity(DbUpdateException)
Attempts to extract the database constraint identity (constraint name and table name)
associated with a DbUpdateException, on a best-effort basis across
SQL Server, PostgreSQL, SQLite, and MySQL/MariaDB providers. Returns
(null, null) when no identity can be determined (unknown provider, missing
inner exception, message format the parser does not recognise, localised provider
message, etc.).
public static (string? ConstraintName, string? TableName) ExtractConstraintIdentity(DbUpdateException ex)
Parameters
exDbUpdateExceptionThe DbUpdateException to extract identity from.
Returns
- (string ConstraintName, string TableName)
A tuple of
(ConstraintName, TableName)where each element may independently be null if the provider did not surface it or the message could not be parsed.
Remarks
Typed extraction is preferred where the provider exposes the names as properties
(PostgreSQL's PostgresException.ConstraintName / TableName); the
SQL Server / SQLite / MySQL paths parse the message because those providers do
not expose typed identity properties.
Telemetry-only. Constraint and table names can reveal schema details and
should not be surfaced directly in API responses. Use the returned values for
structured logging and observability dimensions (for example as the
ConstraintName / ConstraintTableName
payload on the Error.Conflict produced by
DbContext.TryInsertUniqueAsync).
Exceptions
- ArgumentNullException
Thrown when
exis null.
IsDuplicateKey(DbUpdateException)
Returns true if the exception represents a unique constraint violation (duplicate key).
public static bool IsDuplicateKey(DbUpdateException ex)
Parameters
exDbUpdateExceptionThe DbUpdateException to classify.
Returns
- bool
trueif this is a duplicate key violation; otherwisefalse.
IsForeignKeyViolation(DbUpdateException)
Returns true if the exception represents a foreign key constraint violation.
public static bool IsForeignKeyViolation(DbUpdateException ex)
Parameters
exDbUpdateExceptionThe DbUpdateException to classify.
Returns
- bool
trueif this is a foreign key violation; otherwisefalse.