dotnet/efcore · error · InvalidOperationException
The property ' . ' contains null, but the property is…
Error message
The property '{1_entityType}.{0_property}' contains null, but the property is marked as required. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values. What it means
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.
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.
Example fix
// before
public class Order
{
public string Name { get; set; } // non-nullable, left null
}
var o = new Order(); dbContext.Add(o); await dbContext.SaveChangesAsync();
// after
public class Order
{
public string Name { get; set; } = string.Empty;
}
// or modelBuilder.Entity<Order>().Property(x => x.Name).IsRequired(false); Defensive patterns
Strategy: validation
Validate before calling
// Pre-save null check for required properties
foreach (var entry in dbContext.ChangeTracker.Entries())
{
if (entry.State is not (EntityState.Added or EntityState.Modified)) continue;
foreach (var prop in entry.Metadata.GetProperties().Where(p => !p.IsNullable))
{
if (entry.CurrentValues[prop] is null)
throw new InvalidOperationException($"{entry.Metadata.DisplayName()}.{prop.Name} is required but null.");
}
} Try / catch
try { await dbContext.SaveChangesAsync(ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("contains null"))
{
// identify the null required property, set it, then retry.
} Prevention
- Initialize required properties with defaults.
- Run a pre-save validation pass over the ChangeTracker.
- Use non-nullable reference types and provide defaults.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Specified argument was out of the range of valid values…
- The partition key value is of type
- The partition key value supplied for
- The value ' ' provided for argument ' ' must be a valid…
- Unable to generate a valid 'id' value to execute a…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/1a4538f667119479.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Storage/Internal/CosmosStructuralTypeSerializer.cs:428
{
if (!discriminatorProperty.IsShadowProperty())
{
return discriminatorProperty.GetGetter().GetClrValue(instance!);
}
var instanceType = instance!.GetType();
return structuralType.GetDerivedTypesInclusive().First(t => t.ClrType == instanceType).GetDiscriminatorValue();
}
public void SetOrdinal(IProperty ordinalKeyProperty, int? ordinal)
{
}
public void ValidateNull(IProperty property, ITypeBase structuralType)
{
if (!property.IsNullable)
{
throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(property.Name, structuralType.DisplayName()));
}
}
public void ValidateNull(IComplexProperty complexProperty, ITypeBase structuralType)
{
if (!complexProperty.IsNullable)
{
throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(complexProperty.Name, structuralType.DisplayName()));
}
}
public void ValidateNull(INavigation navigation, ITypeBase structuralType)
{
if (navigation.ForeignKey.IsRequired)
{
throw new InvalidOperationException(CoreStrings.PropertyConceptualNull(navigation.Name, structuralType.DisplayName()));
}
}View on GitHub (pinned to 3a2006ef56)