Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer
1.0.0-preview.4
dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer --version 1.0.0-preview.4
NuGet\Install-Package Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer -Version 1.0.0-preview.4
<PackageReference Include="Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer" Version="1.0.0-preview.4" />
<PackageVersion Include="Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer" Version="1.0.0-preview.4" />
<PackageReference Include="Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer" />
paket add Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer --version 1.0.0-preview.4
#r "nuget: Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer, 1.0.0-preview.4"
#:package Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer@1.0.0-preview.4
#addin nuget:?package=Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer&version=1.0.0-preview.4&prerelease
#tool nuget:?package=Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer&version=1.0.0-preview.4&prerelease
Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer
A static library for managing and validating database constraint exceptions specifically for SQL Server.
Overview
This package provides a SQL Server-specific implementation of the constraint validator interface, allowing you to handle integrity exceptions (primary key, foreign key, not null) thrown by SQL Server and convert them into custom exceptions for your application.
Installation
dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer
Usage
1. Register the SQL Server validator
using Tolitech.Infrastructure.Persistence.ConstraintValidator;
using Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer;
ConstraintValidatorManager.AddValidator(new SqlServerConstraintValidator());
2. Handle SQL Server exceptions
try
{
// Database operation
}
catch (Exception ex)
{
Exception handled = ConstraintValidatorManager.Handle(ex);
if (handled is DatabaseConstraintViolationException)
{
// Custom logic for constraint violations
}
}
Supported SQL Server Constraint Violations
- Primary key violation:
Number = 2627
- Foreign key violation:
Number = 547
- Not null violation:
Number = 515
Advanced
You can remove or clear validators:
ConstraintValidatorManager.RemoveValidator(yourValidator);
ConstraintValidatorManager.ClearValidators();
Example: Entity Framework Core
try
{
await dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
var handled = ConstraintValidatorManager.Handle(ex);
if (handled is PrimaryKeyViolationException)
// Notify user about duplicate
}
Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer makes handling SQL Server constraint exceptions simple and robust.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Microsoft.Data.SqlClient (>= 6.0.2)
- Tolitech.Infrastructure.Persistence.ConstraintValidator (>= 1.0.0-preview.5)
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 |
---|---|---|
1.0.0-preview.4 | 38 | 7/18/2025 |
1.0.0-preview.3 | 109 | 7/14/2025 |
1.0.0-preview.2 | 118 | 7/3/2025 |
1.0.0-preview.1 | 89 | 12/12/2024 |