Entity Framework - Storing complex objects as JSON

12/14/2023
2 minute read

From time to time, it is nice to store complex objects or lists as JSON in the database. With Entity Framework 8, this is now easily possible. But this was possible all along with Entity Framework 7.

Entity Framework 8

What is possible since the latest version of Entity Framework (8) is to store lists of simple types as JSON. So, if you have a model like this:

public class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<string> Tags { get; set; }
}

Imagine the following blog post:

var post = new BlogPost
{
    Title = "Entity Framework 8",
    Tags = new List<string> { "Entity Framework", "EF8", "JSON" }
};

Then the tags will be stored as: '["Entity Framework", "EF8", "JSON"]' in the database. Okay - that is very handy. But there are other news to Entity Framework 8: You can also store complex objects as owned types! So if you have a model like this:

public class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<string> Tags { get; set; }
    public Author Author { get; set; }
}

public class Author
{
    public string Name { get; set; }
    public string Email { get; set; }
}

Then in Entity Framework 8 you can configure this in your mapping as:

modelBuilder.Entity<BlogPost>()
    .OwnsOne(p => p.Author);

Which creates column names like 'Author_Name' and 'Author_Email' in the database. Nice: Value objects are now first class citizens in Entity Framework.

Complex objects as JSON

But what if you want to store the author as JSON? This is also possible with Entity Framework. And not only since version 8, but since version 7 (well, technically since forever, as you can use ValueConverters):

modelBuilder.Entity<BlogPost>()
    .OwnsOne(p => p.Author, b => b.ToJson());

This will result in a column named 'Author' in the database, which contains the JSON representation of the author. The cool thing is, that this also allows filtering, sorting and so on!

Resources

Entity Framework and ordered indexes

In Entity Framework 7, the team has added support for ordered indexes to the fluent API. In this blog post we will look at how to use this feature and what it means for your database.

Entity Framework 8: Raw SQL queries on unmapped types

The next iteration of Entity Framework, namely Entity Framework 8, will have a new and exciting feature:

Support raw SQL queries without defining an entity type for the result

That means less boilerplate code!

Native DateOnly and TimeOnly support in Entity Framework 8 in SQL Server

.NET 6 brought us two new datatypes: DateOnly and TimeOnly. For those types we don't have any first class support in Entity Framework - until now.

There is a recent change, that hit us with Entity Framework 8 that might ease the situation and brings native support for those types.

An error has occurred. This application may no longer respond until reloaded. Reload x