From 36d06faa77e0c73988d6a008339fea71fb243e7d Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Mon, 28 Nov 2022 10:32:22 -0800 Subject: [PATCH] Update post.md --- .../2022-11-25-JSON-columns/post.md | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-25-JSON-columns/post.md b/docs/en/Community-Articles/2022-11-25-JSON-columns/post.md index 95a7754936..cb8ceafc88 100644 --- a/docs/en/Community-Articles/2022-11-25-JSON-columns/post.md +++ b/docs/en/Community-Articles/2022-11-25-JSON-columns/post.md @@ -1,23 +1,22 @@ -# The new EF Core JSON Columns +# JSON Columns in Entity Framework Core 7 -In this article, we will see how to use the new JSON columns in EF Core 7.0. +In this article, we will see how to use the new **JSON Columns** features that came with EF Core 7 in an ABP based application (with examples). ## JSON Columns -Most relational databases support columns that contain JSON documents. The JSON in these columns can be drilled into with queries. This allows, for example, filtering and sorting by the elements of the documents, as well as projection of elements out of the documents into results. JSON columns allow relational databases to take on some of the characteristics of document databases, creating a useful hybrid between the two. +Most relational databases support columns that contain JSON documents. The JSON in these columns can be drilled into with queries. This allows, for example, filtering and sorting by the elements of the documents, as well as projection of elements out of the documents into results. JSON columns allow relational databases to take on some of the characteristics of document databases, creating a useful hybrid between these two database management approaches. -EF7 contains provider-agnostic support for JSON columns, with an implementation for SQL Server. This support allows mapping of aggregates built from .NET types to JSON documents. Normal LINQ queries can be used on the aggregates, and these will be translated to the appropriate query constructs needed to drill into the JSON. EF7 also supports updating and saving changes to the JSON documents. +EF7 contains provider-agnostic support for JSON columns, with an implementation for SQL Server. This support allows the mapping of aggregates built from .NET types to JSON documents. Normal LINQ queries can be used on the aggregates, and these will be translated to the appropriate query constructs needed to drill into the JSON. EF7 also supports updating and saving changes to JSON documents. -You can find more information about JSON columns in EF Core 7.0 in the [documentation](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns). +> You can find more information about JSON columns in EF Core's [documentation](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns). ### Mapping JSON Columns -In EF Core, aggregate types are defined using `OwnsOne` and `OwnsMany`. -`OwnsOne` is used to map a single aggregate, and `OwnsMany` is used to map a collection of aggregates. +In EF Core, aggregate types can be defined using `OwnsOne` and `OwnsMany` methods. `OwnsOne` can be used to map a single aggregate and the `OwnsMany` method can be used to map a collection of aggregates. -`ToJson` is used to map a property to a JSON column. The property can be of any type that can be serialized to JSON. +With EF 7, we have a new extension method for mapping property to a JSON Column: `ToJson`. We can use this method to mark a property as a JSON Column. The property can be of any type that can be serialized to JSON. -These aggregates can be mapped to JSON columns using the `ToJson` method. The following example shows how to map a JSON column to an aggregate type: +The following example shows how to map a JSON column to an aggregate type: ```csharp public class ContactDetails @@ -49,7 +48,8 @@ public class Person : AggregateRoot } ``` -Above, we have defined an aggregate type `ContactDetails` that contains an `Address` and a `Phone` number. The aggregate type is configured in `OnModelCreating` using `OwnsOne` and `ToJson`. The `Address` property is mapped to a JSON column using `ToJson`, and the `Phone` property is mapped to a regular column. This requires just one call to ToJson() when configuring the aggregate type: +* Above, we have defined an aggregate type `ContactDetails` that contains an `Address` and a `Phone` number. The aggregate type is configured in `OnModelCreating` using `OwnsOne` and `ToJson` methods below. +* The `Address` property is mapped to a JSON column using `ToJson`, and the `Phone` property is mapped to a regular column. This requires just one call to **ToJson()** when configuring the aggregate type: ```csharp @@ -72,7 +72,7 @@ public class MyDbContext : AbpDbContext b.ConfigureByConvention(); b.OwnsOne(x=>x.ContactDetails, c => { - c.ToJson(); + c.ToJson(); //mark as JSON Column c.OwnsOne(cd => cd.Address); }); }); @@ -80,39 +80,30 @@ public class MyDbContext : AbpDbContext } ``` - - ### Querying JSON Columns -Queries into JSON columns work just the same as querying into any other aggregate type in EF Core. That is, just use LINQ! Here are some examples: +Queries into JSON columns work just the same as querying into any other aggregate type in EF Core. That's it, just use the LINQ! Here are some examples: ```csharp var persons = await (await GetDbSetAsync()).ToListAsync(); -``` - -```csharp var contacts = await (await GetDbSetAsync()).Select(person => new { person, - person.ContactDetails.Phone, - Addresses = person.ContactDetails.Address + person.ContactDetails.Phone, //query over JSON column + Addresses = person.ContactDetails.Address //query over JSON column }).ToListAsync(); -``` - - -```csharp var addresses = await (await GetDbSetAsync()).Select(person => new { person, - Addresses = person.ContactDetails.Address + Addresses = person.ContactDetails.Address //query over JSON column }).ToListAsync(); ``` ### Updating JSON Columns -You can update JSON columns using the `Update` method. The following example shows how to update a JSON column: +You can update JSON columns the same as updating any record by using the `UpdateAsync` method. The following example shows how to update a JSON column: ```csharp var person = await (await GetDbSetAsync()).FirstAsync(); @@ -122,12 +113,19 @@ person.ContactDetails.Address = new Address("Street", "City", "Postcode", "Count await UpdateAsync(person, true); ``` -### Database View +### JSON Column in a Database + +After you've configured the database relations, created a new migration and applied it to database you will have a database table like below: ![image](./Database.png) -### References +As you can see, thanks to JSON Columns feature the **ContactDetails** row has JSON content and we can use it in a query or update it from our application with the LINQ JSON query support that mentioned above. -[https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns) +### Conclusion + +In this article, I've briefly introduced the JSON Columns feature that was shipped with EF Core 7. It's pretty straightforward to use JSON Columns in an ABP based application. You can see the examples above and give it a try! + +### References -[https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities](https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities) \ No newline at end of file +* [https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns) +* [https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities](https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities) \ No newline at end of file