From 0b2d727965f2fa0085ab820ccf5d7c18c79294bc Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 25 Nov 2022 18:26:22 +0300 Subject: [PATCH 1/3] Add Json-Column Article --- .../2022-11-25-JSON-columns/Database.png | Bin 0 -> 10172 bytes .../2022-11-25-JSON-columns/post.md | 98 ++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 docs/en/Community-Articles/2022-11-25-JSON-columns/Database.png create mode 100644 docs/en/Community-Articles/2022-11-25-JSON-columns/post.md diff --git a/docs/en/Community-Articles/2022-11-25-JSON-columns/Database.png b/docs/en/Community-Articles/2022-11-25-JSON-columns/Database.png new file mode 100644 index 0000000000000000000000000000000000000000..c8a5516aa6ed8bc9070ffa772a3645abe0b837a5 GIT binary patch literal 10172 zcmd^lcT`hL*Ea|%S4310lu$$g1u3FHKuWA2(gYQisv?~b>46YpxqyXo73n3?K?I~F zln_u9gb*S{0t5&UAyQIk2_%qw=zX8}-sis0yS}x)f4{7ilbJL7%{dnC^PJT=0fDMS z(T%&?x9Yz=xO_WEKtM|Q=U33&PKhZXaLm^H;<@Wk7g}~x!c6_Qig}N$GxNLDvix>R z80ay9G9`DYITr5$Uacjq!wL+qrYAXK*J~Mf&&-ybZ`-qbr<>tPl|Gy21)tiEGz71` z#o9#3Ph3h>l9!Se-+#00dKiO8^a*Y{GO=k|jpeah`0*8%d=9Y%%9a01{anBSK?ZQYAY(q62Jo)_ z`}h@(!Wi;pVNB&sVa&e2Om4K&*aPJ+Bl%7mrI?!M5812F9|HesK94TFA;@50x&BoV zf($JMdQ4N(b2=kd+!gz5UGuG$g5@T=tr$XIBNfAfHtw#yDt62*6lAP{_V~P7%AGzk z5&tjGD*pZzJlD8woQ;fue6EAErD}hT1V*mCa&3BFp+2FpuKV;MJL;eEJGPD6lVfby zGn`nhk9DGRyM)GBWT#2YYr^b2b1@s?$Q$dJoS&Mf?iYmo&DZ|d%7n)4TI<5plcfjt zKykztIOpVOEFoufOkBal9cjGI@?N_mG_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W`0`gOEZ&XO>z}m0XLi5!QW?EefbZM5>{_`4uQ!c;BuD39`nU-t2LT z)i68mE{mh-uhK3oa{!NIKY|?KUt-qD?0F#AZYNWPN6yN5P$D&ZT~agGRt|u z@um3&Pz2Fk*tK+Pn_g>xHva93%=1NKegwjfo!0aHW=L%j(rwTE;^OAP+m>VfP@SM| zC!3CUp~l@CUuBN2%_PHXx?_!7R*3t&t-o*H>%yP8)UU9)T2~7pb5CJM38@K-w-0j@ z)_=s_>zkv#Y^O)bbh7t*`c~%Fi^~)Q?Y!UV0^O5yJ9&Aa;POmvX4$iAW5(@qiEDti zd;CyjsV>V;%d;1&bSSC8xChv|dS9-e>v3M!)OWd|_E@>a&+zT%G_;!Q9#rS-ZBTA( z-t4^R+t6&ec3yTPihc?k&FMF)fQ$x_0~PSK9al>%{C#iV4CnjTsO~w++Bdq(f^Op( z4S~8|M)6KpdFO72YAmd|G0Q0$RQfHpm{FWY-LfTR(VoN9xHHV3W|0wwE=A~_2HCU*Zjyxm@oZV2i_F{E-MRJOJ3j{jU-a~)=ERE{j)_~>lc zJwqEEc}Sr+d0V@f?6&sls+W(C^fd^bKPxCuk!}@`mb4Wdqq*09rk%puxFNlX66_No z%y!|*L^;^#SK_j15D|f+&!xtF4$bj08o#1|-5v+f7mmZ_Q=&9Ln;Y@L^rw5T3Ow}n zu#lZmpI@_``#;~Jqj~o1S Og83!uixubZJpMl&1{MDR literal 0 HcmV?d00001 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 new file mode 100644 index 0000000000..f8dd2c4dec --- /dev/null +++ b/docs/en/Community-Articles/2022-11-25-JSON-columns/post.md @@ -0,0 +1,98 @@ +# The new EF Core JSON Columns + +## 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. + +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. + +### Mapping JSON Columns AbpDbContext + +```csharp +public class ContactDetails +{ + public Address Address { get; set; } + public string? Phone { get; set; } +} + +public class Address +{ + public Address(string street, string city, string postcode, string country) + { + Street = street; + City = city; + Postcode = postcode; + Country = country; + } + + public string Street { get; set; } + public string City { get; set; } + public string Postcode { get; set; } + public string Country { get; set; } +} + +public class Person : AggregateRoot +{ + public string Name { get; set; } = null!; + public ContactDetails ContactDetails { get; set; } = null!; +} + +public class MyDbContext : AbpDbContext +{ + public DbSet Persons { get; set; } + + public MyDbContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.Entity(b => + { + b.ToTable(abpEf7JsonColumnConsts.DbTablePrefix + "Persons", abpEf7JsonColumnConsts.DbSchema); + b.ConfigureByConvention(); + b.OwnsOne(x=>x.ContactDetails, c => + { + c.ToJson(); + c.OwnsOne(cd => cd.Address); + }); + }); + } +} +``` + +### Querying JSON Columns + +```csharp +var persons = await (await GetDbSetAsync()).ToListAsync(); + +var contacts = await (await GetDbSetAsync()).Select(person => new +{ + person, + person.ContactDetails.Phone, + Addresses = person.ContactDetails.Address +}).ToListAsync(); + +var addresses = await (await GetDbSetAsync()).Select(person => new +{ + person, + Addresses = person.ContactDetails.Address +}).ToListAsync(); +``` + +### Updating JSON Columns + +```csharp +var person = await (await GetDbSetAsync()).FirstAsync(); + +person.ContactDetails.Phone = "123456789"; +person.ContactDetails.Address = new Address("Street", "City", "Postcode", "Country"); +await UpdateAsync(person, true); +``` + +### Database View + +![image](./Database.png) From 209f23b85e81b590523be10cb4d34c693cb4bf80 Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 25 Nov 2022 23:21:28 +0300 Subject: [PATCH 2/3] Update post.md --- .../2022-11-25-JSON-columns/post.md | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 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 f8dd2c4dec..95a7754936 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,12 +1,23 @@ # The new EF Core JSON Columns +In this article, we will see how to use the new JSON columns in EF Core 7.0. + ## 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. 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. -### Mapping JSON Columns AbpDbContext +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). + +### 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. + +`ToJson` is used to map a property to 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: ```csharp public class ContactDetails @@ -36,6 +47,11 @@ public class Person : AggregateRoot public string Name { get; set; } = null!; public ContactDetails ContactDetails { get; set; } = null!; } +``` + +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: + +```csharp public class MyDbContext : AbpDbContext { @@ -52,7 +68,7 @@ public class MyDbContext : AbpDbContext builder.Entity(b => { - b.ToTable(abpEf7JsonColumnConsts.DbTablePrefix + "Persons", abpEf7JsonColumnConsts.DbSchema); + b.ToTable(MyProjectConsts.DbTablePrefix + "Persons", MyProjecConsts.DbSchema); b.ConfigureByConvention(); b.OwnsOne(x=>x.ContactDetails, c => { @@ -64,17 +80,28 @@ 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: + ```csharp var persons = await (await GetDbSetAsync()).ToListAsync(); +``` + +```csharp var contacts = await (await GetDbSetAsync()).Select(person => new { person, person.ContactDetails.Phone, Addresses = person.ContactDetails.Address }).ToListAsync(); +``` + + +```csharp var addresses = await (await GetDbSetAsync()).Select(person => new { @@ -85,6 +112,8 @@ var addresses = await (await GetDbSetAsync()).Select(person => new ### Updating JSON Columns +You can update JSON columns using the `Update` method. The following example shows how to update a JSON column: + ```csharp var person = await (await GetDbSetAsync()).FirstAsync(); @@ -96,3 +125,9 @@ await UpdateAsync(person, true); ### Database View ![image](./Database.png) + +### References + +[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 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 3/3] 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