Skip to main content
Version: 0.3.x (0.3.0)

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 and Pattern to . The name is read off the error, never off the model: a compiled model holds no check constraint.

Only an error reporting a violated CHECK constraint is read: PostgreSQL's SQL state 23514, whose constraint Npgsql names in ; SQL Server's error 547, whose number SqlClient states in in every language, the name read off its message, quoted or not; SQLite's 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​

bool

true when a code was found.

Remarks​

Each exception is read in turn, from exception down its chain, since Entity Framework Core fails SaveChanges with a DbUpdateException around the provider's error; the first reporting a violated constraint of the convention gives the code.

It reads the error whoever sent the command: Entity Framework Core without , Dapper, a bulk library or ADO.NET. With it, finds the same code, which the interceptor stores in the provider's error, but on an error Entity Framework Core hands no interceptor: on SQL Server, that of a raw SQL query writing rows it reads back, SqlQuery or FromSql over OUTPUT, which SqlClient reports while the rows are read. This reads the code there too.

Exceptions​

ArgumentNullException

exception is null.