EF Core validation
SQL Server validation
Prevent "String or binary data would be truncated" error
Entity Framework Core interceptor
Database schema validation
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.
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
- 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
SaveChangesinterceptor - Built-in logging support
- Validation diagnostics metrics
- Clear developer-friendly error messages
- .NET 8+
- Entity Framework Core
- SQL Server
Install via NuGet Package Manager
Install-Package DBGuard
or using .NET CLI
dotnet add package DBGuard
Follow these steps to start using DBGuard.
Add DBGuard to your service collection.
builder.Services.AddDbGuard(connectionString);This loads the database schema and registers the validation engine.
builder.Services.AddDbContext<AppDbContext>((sp, options) =>
{
var interceptor = sp.GetRequiredService<DbGuardInterceptor>();
options.UseSqlServer(connectionString)
.AddInterceptors(interceptor);
});DBGuard will now automatically validate entities before saving.
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
}CREATE TABLE Users
(
Id INT IDENTITY PRIMARY KEY,
FirstName VARCHAR(20),
Email VARCHAR(50)
)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();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.
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
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.
DBGuard currently supports:
- String length validation
- Null validation
- Data type validation
- Decimal precision validation
- Numeric overflow validation
- Date validation
DBGuard
│
├── DBGuard.Core
├── DBGuard.Schema
├── DBGuard.Validation
├── DBGuard.SqlServer
├── DBGuard.EFCore
│
├── samples
│ └── DBGuard.Demo
│
└── tests
└── DBGuard.Tests
Without DBGuard:
SqlException: String or binary data would be truncated
With DBGuard:
DBGuard Validation Failed
Table: Products
Column: Description
Allowed Length: 100
Actual Length: 140
Developers immediately know which column caused the issue.
Ensure your connection string is correct and you are using either:
Windows Authentication:
Trusted_Connection=True
or SQL Authentication:
User Id=yourUser;
Password=yourPassword;
Do not use both together.
Ensure the table exists in the database and DBGuard can access it.
Future improvements:
- PostgreSQL support
- MySQL support
- SQL error analyzer
- Performance optimizations
- Monitoring dashboard
- Additional validation rules
Contributions are welcome.
- Fork the repository
- Create a feature branch
- Submit a pull request
Read full explanation here: (https://dev.to/birupakhya_dash_b781f4db3/fixing-the-string-or-binary-data-would-be-truncated-error-in-ef-core-2dji)
MIT License
Birupakhya Dash
serious open-source library.