DBGuard 1.0.0

dotnet add package DBGuard --version 1.0.0
                    
NuGet\Install-Package DBGuard -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DBGuard" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DBGuard" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DBGuard" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DBGuard --version 1.0.0
                    
#r "nuget: DBGuard, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DBGuard@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DBGuard&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DBGuard&version=1.0.0
                    
Install as a Cake Tool

DBGuard

DBGuard is a schema-aware validation library for Entity Framework Core that prevents common SQL Server errors before data reaches the database.

It automatically validates entity data against the actual database schema and throws clear, developer-friendly validation errors instead of cryptic SQL exceptions.


Why DBGuard?

When saving data using EF Core, SQL Server often throws errors like:

String or binary data would be truncated

This error does not tell you:

  • Which table failed
  • Which column caused the issue
  • What the allowed size is
  • What the actual value size is

DBGuard solves this problem by validating data before the database insert happens.

Example DBGuard error:

DBGuard Validation Failed

Table: Users
Column: FirstName
Allowed Length: 20
Actual Length: 45

Suggestion: Trim input or increase column length

Features

  • Schema-aware validation using actual database schema
  • Automatic validation before SaveChanges
  • Column length validation
  • Null constraint validation
  • Data type validation
  • Decimal precision validation
  • Numeric overflow validation
  • Date validation
  • EF Core SaveChanges interceptor
  • Built-in logging support
  • Validation diagnostics metrics
  • Clear and developer-friendly error messages

Requirements

  • .NET 8+
  • Entity Framework Core
  • SQL Server

Installation

Install via NuGet Package Manager

Install-Package DBGuard

or using .NET CLI

dotnet add package DBGuard

Quick Start

1. Configure DBGuard

Add DBGuard to your service collection.

builder.Services.AddDbGuard(connectionString);

This loads the database schema and registers the validation engine.


2. Register EF Core DbContext

builder.Services.AddDbContext<AppDbContext>((sp, options) =>
{
    var interceptor = sp.GetRequiredService<DbGuardInterceptor>();

    options.UseSqlServer(connectionString)
           .AddInterceptors(interceptor);
});

DBGuard will now automatically validate entities before saving.


3. Example Entity

public class User
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string Email { get; set; }
}

4. Example Database Table

CREATE TABLE Users
(
    Id INT IDENTITY PRIMARY KEY,
    FirstName VARCHAR(20),
    Email VARCHAR(50)
)

5. Example Insert

var user = new User
{
    FirstName = "THIS_IS_A_VERY_LONG_NAME_EXCEEDING_COLUMN_LIMIT",
    Email = "test@test.com"
};

context.Users.Add(user);

await context.SaveChangesAsync();

Example Error Output

Instead of SQL Server throwing:

String or binary data would be truncated

DBGuard throws:

DBGuard Validation Failed

Table: Users
Column: FirstName
Allowed Length: 20
Actual Length: 45

Suggestion: Trim input or increase column length

This makes debugging significantly easier.


Logging

DBGuard integrates with Microsoft.Extensions.Logging.

Example configuration:

builder.Services.AddLogging(config =>
{
    config.AddConsole();
});

Example log output:

warn: DBGuardInterceptor

DBGuard validation failed.
Table: Users
Column: FirstName
Error: FirstName exceeds max length 20

Diagnostics Metrics

DBGuard tracks frequently failing columns to help diagnose issues.

Example usage:

var metrics = serviceProvider.GetRequiredService<ValidationMetrics>();

var failures = metrics.GetTopFailures();

foreach (var f in failures)
{
    Console.WriteLine($"{f.Key} -> {f.Value}");
}

Example output:

Users.FirstName -> 3 failures
Users.Email -> 1 failure

This helps identify problematic fields in large systems.


Supported Validations

DBGuard currently supports:

  • String length validation
  • Null validation
  • Data type validation
  • Decimal precision validation
  • Numeric overflow validation
  • Date validation

Contributing

Contributions are welcome. Please submit pull requests or open issues.


License

MIT License


Author

Birupakhya Dash


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 116 3/10/2026