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
WorkFlowJSON includes a non-nullblocksarray 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();
-
Please edit your post to ask only one question, not multiple questions.Progman– Progman2025年08月01日 16:49:35 +00:00Commented Aug 1, 2025 at 16:49
-
Show full callstack.Svyatoslav Danyliv– Svyatoslav Danyliv2025年08月01日 18:09:46 +00:00Commented 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.jdweng– jdweng2025年08月01日 20:19:10 +00:00Commented Aug 1, 2025 at 20:19
1 Answer 1
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.
Comments
Explore related questions
See similar questions with these tags.