{"record":{"id":"1a4538f667119479","repo":"dotnet/efcore","slug":"the-property-1-entitytype-0-property-contain","errorCode":null,"errorMessage":"The property '{1_entityType}.{0_property}' contains null, but the property is marked as required.  Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.","messagePattern":"The property '(.+?)\\.(.+?)' contains null, but the property is marked as required\\.  Consider using 'DbContextOptionsBuilder\\.EnableSensitiveDataLogging' to see the key values\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/CosmosStructuralTypeSerializer.cs","lineNumber":428,"sourceCode":"        {\n            if (!discriminatorProperty.IsShadowProperty())\n            {\n                return discriminatorProperty.GetGetter().GetClrValue(instance!);\n            }\n\n            var instanceType = instance!.GetType();\n            return structuralType.GetDerivedTypesInclusive().First(t => t.ClrType == instanceType).GetDiscriminatorValue();\n        }\n\n        public void SetOrdinal(IProperty ordinalKeyProperty, int? ordinal)\n        {\n        }\n\n        public void ValidateNull(IProperty property, ITypeBase structuralType)\n        {\n            if (!property.IsNullable)\n            {\n                throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(property.Name, structuralType.DisplayName()));\n            }\n        }\n\n        public void ValidateNull(IComplexProperty complexProperty, ITypeBase structuralType)\n        {\n            if (!complexProperty.IsNullable)\n            {\n                throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(complexProperty.Name, structuralType.DisplayName()));\n            }\n        }\n\n        public void ValidateNull(INavigation navigation, ITypeBase structuralType)\n        {\n            if (navigation.ForeignKey.IsRequired)\n            {\n                throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(navigation.Name, structuralType.DisplayName()));\n            }\n        }","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Storage/Internal/CosmosStructuralTypeSerializer.cs#L410-L446","documentation":"During serialization, ValidateNull(IProperty, ITypeBase) is called for each property. If the property is non-nullable (IsNullable == false) but its value on the instance is null, the provider throws CoreStrings.PropertyConceptualNull. This is the standard 'required property is null' EF error surfaced through the Cosmos serializer's null check.","triggerScenarios":"Serializing an entity instance where a non-nullable property (value type or required reference) holds null. The Cosmos serializer's ValidateNull(IProperty,...) at CosmosStructuralTypeSerializer.cs:428 detects it and throws before writing the document.","commonSituations":"Creating an entity without initializing a required field. Partial deserialization or partial updates that leave a required property unset. Nullable annotations missing on the CLR type so EF treats a reference type as required.","solutions":["Ensure the required property always has a non-null value before SaveChanges (validate in the entity or a pre-save hook).","Make the property nullable if the domain permits nulls (.IsRequired(false) or a nullable CLR type).","Add a default value via .HasDefaultValue or .HasData so the property is never null."],"exampleFix":"// before\npublic class Order\n{\n    public string Name { get; set; } // non-nullable, left null\n}\nvar o = new Order(); dbContext.Add(o); await dbContext.SaveChangesAsync();\n\n// after\npublic class Order\n{\n    public string Name { get; set; } = string.Empty;\n}\n// or modelBuilder.Entity<Order>().Property(x => x.Name).IsRequired(false);","handlingStrategy":"validation","validationCode":"// Pre-save null check for required properties\nforeach (var entry in dbContext.ChangeTracker.Entries())\n{\n    if (entry.State is not (EntityState.Added or EntityState.Modified)) continue;\n    foreach (var prop in entry.Metadata.GetProperties().Where(p => !p.IsNullable))\n    {\n        if (entry.CurrentValues[prop] is null)\n            throw new InvalidOperationException($\"{entry.Metadata.DisplayName()}.{prop.Name} is required but null.\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try { await dbContext.SaveChangesAsync(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"contains null\"))\n{\n    // identify the null required property, set it, then retry.\n}","preventionTips":["Initialize required properties with defaults.","Run a pre-save validation pass over the ChangeTracker.","Use non-nullable reference types and provide defaults."],"tags":["cosmos","serialization","required-property","null","validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}