Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

4 of 4
deleted 99 characters in body
Olivier Jacot-Descombes
  • 114.3k
  • 14
  • 149
  • 202

JSON column mapping – navigation property throws NullReferenceException

I have a model with a WorkFlow column in the database, defined as nvarchar(MAX) and storing JSON data. Previously, we treated it as a plain string in our C# entity, but I'm now trying to take advantage of EF Core 8’s JSON column mapping feature to deserialize it directly into a strongly typed object.

Here's the WorkFlow model class:

public class WorkFlow
{
 public WorkFlow()
 {
 Blocks = new List<Block>();
 Connections = new List<PortConnection>();
 }
 [JsonPropertyName("workFlowId")]
 public Guid WorkFlowId { get; set; }
 [JsonPropertyName("blocks")]
 public List<Block> Blocks { get; set; }
 [JsonPropertyName("connections")]
 public List<PortConnection> Connections { get; set; }
}

EF Core 8 mapping configuration:

public class LogicPointWorkFlowDetailConfiguration : IEntityTypeConfiguration<LogicPointWorkFlowDetail>
{
 public void Configure(EntityTypeBuilder<LogicPointWorkFlowDetail> entity)
 {
 entity.OwnsOne(w => w.WorkFlow, navBuilder =>
 {
 navBuilder.ToJson();
 navBuilder.OwnsMany(x => x.Connections, connectionsBuilder =>
 {
 connectionsBuilder.ToJson();
 });
 navBuilder.OwnsMany(x => x.Blocks, blocksBuilder =>
 {
 blocksBuilder.ToJson();
 blocksBuilder.OwnsOne(b => b.View, viewBuilder =>
 {
 viewBuilder.ToJson();
 viewBuilder.OwnsMany(x => x.PortInputs, portBuilder =>
 {
 portBuilder.ToJson();
 });
 viewBuilder.OwnsMany(x => x.PortOuts, portBuilder =>
 {
 portBuilder.ToJson();
 });
 });
 });
 });
 }
}

The Problem

When I attempt to query the Blocks property (for example, using .Any() in a LINQ expression), I encounter the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.

What I've verified

  • The JSON stored in the database is valid and deserializes correctly outside EF Core
  • Every WorkFlow JSON includes a non-null blocks array with valid elements
  • The model and EF mapping seem to align with the structure of the JSON

Questions

  • Is this a known limitation or bug with EF Core 8's JSON column support?
  • Is there an issue with my usage of .OwnsMany() or how I'm structuring the nested JSON mapping?
  • Should I be initializing the collections in some special way for EF to handle them correctly?

Any help or insights would be greatly appreciated!


I tried to query the database table LogicPointWorkFlowDetails where the WorkFlow column is, and it does work. I can see the WorkFlow json deserilized correctly.

 var activeDetails = await ctx.LogicPointWorkFlowDetails
 .Where(x => x.Status == Lms.UnifiedContextDb.Models.Enums.WorkFlowStatus.Active)
 .ToListAsync();

If i try to execute .Any() on the Blocks property of the Workflow i get the exception. I tried to filter the Blocks with not null but i still get the exception:

var activeDetails1 = await ctx.LogicPointWorkFlowDetails
 .Where(x => x.Status == Lms.UnifiedContextDb.Models.Enums.WorkFlowStatus.Active)
 .Where(x => x.WorkFlow != null
 && x.WorkFlow.Blocks != null
 && x.WorkFlow.Blocks.Any(g => g.LogicPointWorkFlowBlockId != null
 && blockIds.Contains(g.LogicPointWorkFlowBlockId)))
 .Select(x => x.WorkFlow)
 .ToListAsync();
lang-cs

AltStyle によって変換されたページ (->オリジナル) /