dotnet/efcore · error · DbUpdateException

Required properties ' ' are missing for the instance of…

Error message

Required properties '{requiredProperties}' are missing for the instance of entity type '{entityType}' with the key value '{keyValue}'.

What it means

InMemoryTable.ThrowNullabilityErrorException is called when saving an entity instance that has one or more required (non-nullable) properties set to null. With EnableSensitiveDataLogging enabled (_sensitiveLoggingEnabled is true), the error includes the entity's primary key value via BuildCurrentValuesString to help identify the offending row. A DbUpdateException (not InvalidOperationException) is thrown with the entry attached.

Solutions

  1. Ensure every required property on the entity is assigned a non-null value before Add/SaveChanges.
  2. If null is a valid state, configure the property as nullable (IsRequired(false) or a nullable CLR type).
  3. Set a default value via HasDefaultValue or .HasData so the property is never null.
  4. Review the entity's required properties listed in the error message and fix the specific ones that are null.

Example fix

// before — required Name is null
var blog = new Blog { Url = "https://..." }; // Name is required
ctx.Blogs.Add(blog);
ctx.SaveChanges(); // throws

// after — set all required properties
var blog = new Blog { Url = "https://...", Name = "My Blog" };
ctx.Blogs.Add(blog);
ctx.SaveChanges();
Defensive patterns

Strategy: validation

Validate before calling

// Before SaveChanges, validate that all required properties are non-null.
static void ValidateRequiredProperties(DbContext context)
{
    foreach (var entry in context.ChangeTracker.Entries()
        .Where(e => e.State is EntityState.Added or EntityState.Modified))
    {
        foreach (var prop in entry.Metadata.GetProperties())
        {
            if (!prop.IsNullable && entry.CurrentValues[prop] is null)
            {
                throw new ValidationException(
                    $"{entry.EntityType.DisplayName()}.{prop.Name} is required but null. " +
                    $"Key: {entry.BuildCurrentValuesString(entry.Metadata.FindPrimaryKey()!.Properties)}");
            }
        }
    }
}

Try / catch

try
{
    context.SaveChanges();
}
catch (DbUpdateException ex) when (ex.Message.Contains("Required properties"))
{
    // Log the entity and fix the null required properties
    logger.LogError(ex, "Required properties missing on save: {Message}", ex.Message);
    var entry = ex.Entries.FirstOrDefault();
    // Fix or report the problematic entity
    throw;
}

Prevention

When it happens

Trigger: Calling SaveChanges or SaveChangesAsync when at least one tracked entity instance has a null value for a property configured as required (non-nullable), while EnableSensitiveDataLogging() is turned on in DbContextOptions. The InMemory store validates nullability before persisting and surfaces the offending properties and key value.

Common situations: Forgetting to set a required property (e.g. a Name or CreatedAt field) before adding an entity. Making a previously-nullable property required in the model while old data or code paths still produce nulls. Using [Required] or IsRequired() on a CLR reference type whose setter is bypassed. Debug environments where sensitive data logging is enabled.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/499e725b0ad1e8a0. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs:397

    }

    private static bool IsNullable(IProperty property)
        => property.IsNullable
            || (property.DeclaringType is IComplexType complexType
                && IsNullable(complexType.ComplexProperty));

    private static bool IsNullable(IComplexProperty property)
        => property.IsNullable
            || (property.DeclaringType is IComplexType complexType
                && IsNullable(complexType.ComplexProperty));

    private void ThrowNullabilityErrorException(
        IUpdateEntry entry,
        IList<IProperty> nullabilityErrors)
    {
        if (_sensitiveLoggingEnabled)
        {
            throw new DbUpdateException(
                InMemoryStrings.NullabilityErrorExceptionSensitive(
                    nullabilityErrors.Format(),
                    entry.EntityType.DisplayName(),
                    entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties)),
                [entry]);
        }

        throw new DbUpdateException(
            InMemoryStrings.NullabilityErrorException(
                nullabilityErrors.Format(),
                entry.EntityType.DisplayName()),
            [entry]);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that

View on GitHub (pinned to 3a2006ef56)