Class ValueObjectCheckConstraints
Namespace: AdCodicem.ValueObjects.EntityFrameworkCore
Assembly: AdCodicem.ValueObjects.EntityFrameworkCore.dll
Reads the code of the rule a CHECK constraint written by AddValueObjectCheckConstraints refused a row under,
from the error the database provider throws.
public static class ValueObjectCheckConstraints
Inheritance
object ← ValueObjectCheckConstraints
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Remarks
Each constraint the convention writes is named after its rule, CK_{table}_{column}_{rule}, or
CK_{table}_{column}_{rule}_{hash} when the name was shortened to the provider's limit or kept apart from
another, and the rule maps to the code Validate reports for it: MinLength to
Minimum and Maximum to
KnownValues to
Pattern to
Only an error reporting a violated CHECK constraint is read: PostgreSQL's SQL state 23514, whose constraint
Npgsql names in CHECK constraint failed. An error that names a constraint for another reason, such as the "There is already an
object named …" of a migration, carries no code. A constraint declared by hand under a name of the same shape is read
as the convention's, since its name says the same rule; a name holding a space or a quotation mark is read on
PostgreSQL only, which names it apart from its message.
The code is the only thing carried: the column is in the constraint's name unless it was shortened, in SQL Server's
message, and, for SaveChanges, the entity is in DbUpdateException.Entries; neither the value object's
type, which no name holds, nor the value is.
Methods
TryGetCode(Exception, out string?)
Reads the code of the rule a CHECK constraint of AddValueObjectCheckConstraints refused, from the
provider's error or from one an exception wraps.
public static bool TryGetCode(Exception exception, out string? code)
Parameters
exception Exception
The exception, as it was caught: the provider's, or the DbUpdateException around it.
code string?
The code, or null when no exception of the chain reports such a refusal.
Returns
true when a code was found.
Remarks
Each exception is read in turn, from exception down its SaveChanges with a DbUpdateException around the provider's
error; the first
It reads the error whoever sent the command: Entity Framework Core without
SqlQuery or FromSql over OUTPUT,
which SqlClient reports while the rows are read. This reads the code there too.
Exceptions
exception is null.