Entity Framework: Storing complex properties as JSON text in database

403

https://www.codeproject.com/Articles/1166099/Entity-Framework-Storing-complex-properties-as-JSO

Complex properties in entity Framework models such as arrays, dictionaries, and objects can be serialized in SQL Server database in JSON format. In this article I will show you how to map your properties in EF model to database columns that contain JSON.

Background

Entity Framework enables you to map you C# model classes to SQL relational tables. Usually there is one-to-one relationship between model classes and relational tables. If you have properties in the model classes with simple/value types (e.g. int, double, string, boolean) every value type can be easily mapped into columns of the underlying table.

However, what can we do with the complex properties in the model classes, such as arrays and object?

Imagine that you have Blog class in your model that has few value-type properties such as BlogId (int), Url of the blog (string), and relationship to the Post class. Both classes are persisted in database as Blogs and Posts tables.

Now imagine that you want to add in the Blog class some complex properties such as tags (array of strings), or information about the blog owner (separate class).