DbUpdateException 🟪 .NET

DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.

SaveChanges failed in the database; the inner exception (SqlException/PostgresException) has the real reason — constraint violation, truncation, NULL, or deadlock.

Seen on: .NET

Meaning

Always log ex.InnerException. Common inner errors: duplicate key (unique index), FK violation, string or binary data would be truncated, cannot insert NULL.

Common causes

  • Unique constraint violation
  • Foreign key violation
  • String too long for the column
  • NULL in a non-nullable column

⚡ Quick fix

  1. Log the inner exception message
  2. Fix the data or model constraints
  3. Validate before saving

Detailed fix by platform

C#

  1. csharp
    try { await db.SaveChangesAsync(); }
    catch (DbUpdateException ex) { logger.LogError(ex.InnerException, "Save failed: {Msg}", ex.InnerException?.Message); throw; }

How to diagnose

  1. Inner — Exact database error
  2. Entity — ex.Entries list

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.