{"record":{"id":"499e725b0ad1e8a0","repo":"dotnet/efcore","slug":"required-properties-requiredproperties-are-mis","errorCode":null,"errorMessage":"Required properties '{requiredProperties}' are missing for the instance of entity type '{entityType}' with the key value '{keyValue}'.","messagePattern":"Required properties '(.+?)' are missing for the instance of entity type '(.+?)' with the key value '(.+?)'\\.","errorType":"exception","errorClass":"DbUpdateException","httpStatus":null,"severity":"error","filePath":"src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs","lineNumber":397,"sourceCode":"    }\n\n    private static bool IsNullable(IProperty property)\n        => property.IsNullable\n            || (property.DeclaringType is IComplexType complexType\n                && IsNullable(complexType.ComplexProperty));\n\n    private static bool IsNullable(IComplexProperty property)\n        => property.IsNullable\n            || (property.DeclaringType is IComplexType complexType\n                && IsNullable(complexType.ComplexProperty));\n\n    private void ThrowNullabilityErrorException(\n        IUpdateEntry entry,\n        IList<IProperty> nullabilityErrors)\n    {\n        if (_sensitiveLoggingEnabled)\n        {\n            throw new DbUpdateException(\n                InMemoryStrings.NullabilityErrorExceptionSensitive(\n                    nullabilityErrors.Format(),\n                    entry.EntityType.DisplayName(),\n                    entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties)),\n                [entry]);\n        }\n\n        throw new DbUpdateException(\n            InMemoryStrings.NullabilityErrorException(\n                nullabilityErrors.Format(),\n                entry.EntityType.DisplayName()),\n            [entry]);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs#L379-L415","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every required property on the entity is assigned a non-null value before Add/SaveChanges.","If null is a valid state, configure the property as nullable (IsRequired(false) or a nullable CLR type).","Set a default value via HasDefaultValue or .HasData so the property is never null.","Review the entity's required properties listed in the error message and fix the specific ones that are null."],"exampleFix":"// before — required Name is null\nvar blog = new Blog { Url = \"https://...\" }; // Name is required\nctx.Blogs.Add(blog);\nctx.SaveChanges(); // throws\n\n// after — set all required properties\nvar blog = new Blog { Url = \"https://...\", Name = \"My Blog\" };\nctx.Blogs.Add(blog);\nctx.SaveChanges();","handlingStrategy":"validation","validationCode":"// Before SaveChanges, validate that all required properties are non-null.\nstatic void ValidateRequiredProperties(DbContext context)\n{\n    foreach (var entry in context.ChangeTracker.Entries()\n        .Where(e => e.State is EntityState.Added or EntityState.Modified))\n    {\n        foreach (var prop in entry.Metadata.GetProperties())\n        {\n            if (!prop.IsNullable && entry.CurrentValues[prop] is null)\n            {\n                throw new ValidationException(\n                    $\"{entry.EntityType.DisplayName()}.{prop.Name} is required but null. \" +\n                    $\"Key: {entry.BuildCurrentValuesString(entry.Metadata.FindPrimaryKey()!.Properties)}\");\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    context.SaveChanges();\n}\ncatch (DbUpdateException ex) when (ex.Message.Contains(\"Required properties\"))\n{\n    // Log the entity and fix the null required properties\n    logger.LogError(ex, \"Required properties missing on save: {Message}\", ex.Message);\n    var entry = ex.Entries.FirstOrDefault();\n    // Fix or report the problematic entity\n    throw;\n}","preventionTips":["Always set required properties before calling Add() on an entity.","Add a pre-save validation pass over ChangeTracker entries to catch null required properties.","Use nullable reference types and compiler warnings to catch null assignments at compile time.","EnableSensitiveDataLogging in debug/dev to get key values in the error message."],"tags":["efcore","inmemory","validation","required-properties","sensitive-data","savechanges"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}