I’ve been moving Infrastructure of my project to Ef Core 8 from CosmosClient and I’ve been getting exception `System.InvalidOperationException: Nullable object must have a value”, while getting entity that contains List of ValueObjects
My entity:
public sealed class Test : BaseEntity
{
public string ShortName { get; set; } = null!;
public string Name { get; set; } = null!;
public List<Marker> Markers { get; set; } = [];
public Test(string name, string? shortName = null)
{
Name = name;
ShortName = shortName ?? name;
Id = ShortName.EncodePolishLetterAndWhiteChars();
}
public Test() { }
}
Marker:
public sealed class Marker : BaseEntity
{
public Marker() { }
public Marker(string name,
decimal? lowerNorm = null,
decimal? upperNorm = null,
string? unit = null,
string? shortName = null)
{
ShortName = shortName ?? name;
Name = name;
LowerNorm = lowerNorm;
UpperNorm = upperNorm;
Unit = unit;
}
public string ShortName { get; set; } = null!;
public string Name { get; set; } = null!;
public decimal? LowerNorm { get; set; }
public decimal? UpperNorm { get; set; }
public string? Unit { get; set; }
}
DbContext OnModelCreating:
...
modelBuilder
.Entity<Test>()
.ToContainer("Tests")
.HasNoDiscriminator()
.HasPartitionKey(r => r.ShortName);
modelBuilder.Entity<Test>()
.Property(r => r.Id)
.ToJsonProperty("id");
modelBuilder.Entity<Test>()
.ComplexProperty(r => r.Markers)
.IsRequired();
...
I’ve tried making every property nullable, making List nullable and making List of nullable Markers. When I do not set Markers as Complex Property I get Test but List is empty.