Storing Data as Json
The AddJsonProperty
method allows you to define a property that stores an entire object
(of type T
) in the database as a JSON string.
This approach gives you the flexibility to persist structured data without requiring a separate entity or table.
publicpartialclassEmployee:IBkEntity<Employee,ApplicationDbContext>
{
publicvoidBkConfiguration(IBkEntityDesigner<Employee,ApplicationDbContext>designer)
{
designer.Properties.AddJsonProperty<EmployeePreferences>("Preferences");
// ... other properties... }
}
This setup is configured internally like this:
publicclassConfiguration:global::Microsoft.EntityFrameworkCore.IEntityTypeConfiguration<global::BlazorForKidsProjectTemplate.Domain.Entities.Employee>
{
publicvoidConfigure(global::Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<global::BlazorForKidsProjectTemplate.Domain.Entities.Employee>builder)
{
builder.OwnsOne<global::EmployeePreferences>(a=>a.Preferences,x=>x.ToJson());
builder.HasKey(bk=>bk.Id);
builder.Property(bk=>bk.Id).HasConversion(bk=>bk.Value,bk=>newglobal::BlazorForKidsProjectTemplate.Domain.Entities.EmployeeId(bk)).ValueGeneratedNever();
}
}
In this example, the EmployeePreferences
class contains various personal settings such as theme, language, and formatting preferences:
publicclassEmployeePreferences
{
publicstringTheme{get;set;}=string.Empty;
publicstringLanguage{get;set;}=string.Empty;
publicstringTimeZone{get;set;}=string.Empty;
publicstringDateFormat{get;set;}=string.Empty;
publicstringTimeFormat{get;set;}=string.Empty;
publicstringCurrency{get;set;}=string.Empty;
publicstringNumberFormat{get;set;}=string.Empty;
}
Why Use a JSON Property?
Storing data as JSON inside a column provides flexibility in scenarios where:
- You want to store structured or grouped data but don’t want to create a separate entity or table.
- The structure of the data may change over time, and JSON allows you to evolve the model without requiring database migrations.
- You’re working with a custom query or denormalized data that doesn't need full relational mapping. You might precompute this data and use it in the UI for fast access, avoiding the cost of re-running the query.
-
You prefer to manage the value programmatically, using
IBkEventHandler
to update the JSON field when dependent data changes.
When to Use AddJsonProperty
This kind of property is ideal when:
- You are building settings, preferences, filters, or personalization features for a user or entity.
- The data is not relational or is used only by the parent entity and does not need to be queried independently.
- You need a lightweight way to persist complex or semi-structured values without introducing unnecessary database complexity.
- You want to store a denormalized snapshot of a larger query result to improve UI performance or cache frequently accessed data.
How It Works in BlazorForKids
When you define a JSON property:
- The framework serializes and deserializes the object automatically as JSON.
- The data is stored in a single column in the database as a JSON string.
- You can bind its fields in the UI just like any other complex property.
Although the data is persisted as JSON, the structure is still strongly typed, and fully supported in the UI.
This makes AddJsonProperty
a powerful option for building modern apps that need flexibility, performance,
and simplicity — all without sacrificing type safety or clarity in your code.