0

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();
Olivier Jacot-Descombes
114k14 gold badges149 silver badges202 bronze badges
asked Aug 1, 2025 at 15:53
3
  • Please edit your post to ask only one question, not multiple questions. Commented Aug 1, 2025 at 16:49
  • Show full callstack. Commented Aug 1, 2025 at 18:09
  • Check the log files in SSMS which contain better error messages than in c#. You can find the log files in SSMS in the explorer under Management. Commented Aug 1, 2025 at 20:19

1 Answer 1

0

Working with JSON in EF Core 8:

  • The .ToJson() method should only be applied to the root entity, not to every individual object within your JSON model. I removed all the .ToJson() except the first one and it worked!

  • For custom property naming, you can use [JsonPropertyName("myProperty")] to map properties when the JSON field names differ from your model.

Nimantha
6,5476 gold badges32 silver badges78 bronze badges
answered Aug 5, 2025 at 15:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.