|
| 1 | +//This sample shows how to use EF Core JSON columns with ODP.NET and Oracle Database. |
| 2 | +//It creates an owned entity, inserts, queries, updates, and deletes JSON column data. |
| 3 | +//It requires Oracle EF Core 8 or higher. Oracle Database 21c and higher supports JSON columns. |
| 4 | +//Earlier database versions map aggregate types to NCLOB columns instead of JSON columns. |
| 5 | + |
| 6 | +//Specify the user, password, and data source in the connection string below. |
| 7 | + |
| 8 | +using Microsoft.EntityFrameworkCore; |
| 9 | + |
| 10 | +namespace ODPJsonColumns |
| 11 | +{ |
| 12 | + class Program |
| 13 | + { |
| 14 | + public class ContactDetails |
| 15 | + { |
| 16 | + public Address Address { get; set; } = null!; |
| 17 | + public string? Phone { get; set; } |
| 18 | + } |
| 19 | + |
| 20 | + public class Address |
| 21 | + { |
| 22 | + public Address(string street, string city, string postcode, string country) |
| 23 | + { |
| 24 | + Street = street; |
| 25 | + City = city; |
| 26 | + Postcode = postcode; |
| 27 | + Country = country; |
| 28 | + } |
| 29 | + |
| 30 | + public string Street { get; set; } |
| 31 | + public string City { get; set; } |
| 32 | + public string Postcode { get; set; } |
| 33 | + public string Country { get; set; } |
| 34 | + } |
| 35 | + |
| 36 | + public class Author |
| 37 | + { |
| 38 | + public int Id { get; set; } |
| 39 | + public string? Name { get; set; } |
| 40 | + public ContactDetails? Contact { get; set; } |
| 41 | + } |
| 42 | + |
| 43 | + public class AuthorContext : DbContext |
| 44 | + { |
| 45 | + public DbSet<Author> Authors { get; set; } |
| 46 | + |
| 47 | + //To use Oracle database JSON columns, connect to Oracle Database 21c or higher version and |
| 48 | + // specify OracleSQLCompatibility to DatabaseVersion21 or higher. |
| 49 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 50 | + { |
| 51 | + optionsBuilder.UseOracle("User Id=<USER>; Password=<PASSWORD>; Data Source=<DATA SOURCE>" |
| 52 | + , b => b.UseOracleSQLCompatibility(OracleSQLCompatibility.DatabaseVersion21)); ; |
| 53 | + } |
| 54 | + |
| 55 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 56 | + { |
| 57 | + // Configuring the Contact column as a JSON column |
| 58 | + modelBuilder.Entity<Author>().OwnsOne( |
| 59 | + author => author.Contact, ownedNavigationBuilder => |
| 60 | + { |
| 61 | + ownedNavigationBuilder.ToJson(); |
| 62 | + ownedNavigationBuilder.OwnsOne(contactDetails => contactDetails.Address); |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void Main() |
| 68 | + { |
| 69 | + using (var context = new AuthorContext()) |
| 70 | + { |
| 71 | + context.Database.EnsureDeleted(); |
| 72 | + context.Database.EnsureCreated(); |
| 73 | + |
| 74 | + var author1 = new Author() |
| 75 | + { |
| 76 | + Name = "John Smith", |
| 77 | + Contact = new ContactDetails() |
| 78 | + { |
| 79 | + Phone = "555 123 4567", |
| 80 | + Address = new Address("1 Any Street", "Austin", "78741", "US") |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + var author2 = new Author() |
| 85 | + { |
| 86 | + Name = "Kim Jones", |
| 87 | + Contact = new ContactDetails() |
| 88 | + { |
| 89 | + Phone = "555 234 5678", |
| 90 | + Address = new Address("2 Any Street", "Redwood Shores", "94065", "US") |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + /* |
| 95 | + * Contact inserted into the JSON column as the following example: |
| 96 | + * |
| 97 | + * { |
| 98 | + * "Phone": "555 123 4567", |
| 99 | + * "Address": { |
| 100 | + * "City": "Austin", |
| 101 | + * "Country": "US", |
| 102 | + * "Postcode": "78741", |
| 103 | + * "Street": "1 Any Street" |
| 104 | + * } |
| 105 | + * } |
| 106 | + */ |
| 107 | + |
| 108 | + // Insert data |
| 109 | + Console.WriteLine("Inserting authors into table\n"); |
| 110 | + context.Authors.Add(author1); |
| 111 | + context.Authors.Add(author2); |
| 112 | + context.SaveChanges(); |
| 113 | + |
| 114 | + // Query to verify insert |
| 115 | + Console.WriteLine("Authors in table: "); |
| 116 | + var result = context.Authors.ToList(); |
| 117 | + foreach (var author in result) |
| 118 | + { |
| 119 | + Console.WriteLine($"{author.Name}"); |
| 120 | + } |
| 121 | + Console.WriteLine(); |
| 122 | + |
| 123 | + // Query using owned entity details from JSON column |
| 124 | + var result1 = context.Authors.Where(author => author.Contact.Address.City == "Austin").First(); |
| 125 | + Console.WriteLine($"Author residing in Austin: {result1.Name}\n"); |
| 126 | + |
| 127 | + // Update second author's phone number details |
| 128 | + Console.WriteLine("Updating second author's phone number"); |
| 129 | + author2.Contact.Phone = "123 456 7890"; |
| 130 | + context.SaveChanges(); |
| 131 | + |
| 132 | + // Query to verify update |
| 133 | + var result2 = context.Authors.Where(author => author.Name == "Kim Jones").First(); |
| 134 | + Console.WriteLine($"Retrieve author's new phone number {result2.Name}: {result2.Contact.Phone}\n"); |
| 135 | + |
| 136 | + // Delete second author |
| 137 | + Console.WriteLine("Deleting author with street address '2 Any Street' from table"); |
| 138 | + context.Remove(context.Authors.Single(author => author.Contact.Address.Street == "2 Any Street")); |
| 139 | + context.SaveChanges(); |
| 140 | + Console.WriteLine("Delete complete.\n"); |
| 141 | + |
| 142 | + // Query to verify delete |
| 143 | + Console.WriteLine("Authors remaining in table: "); |
| 144 | + var result3 = context.Authors.ToList(); |
| 145 | + foreach(var author in result3) |
| 146 | + { |
| 147 | + Console.WriteLine($"{author.Name}"); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. */ |
| 155 | + |
| 156 | +/****************************************************************************** |
| 157 | + * |
| 158 | + * You may not use the identified files except in compliance with The MIT |
| 159 | + * License (the "License.") |
| 160 | + * |
| 161 | + * You may obtain a copy of the License at |
| 162 | + * https://github.com/oracle/dotnet-db-samples/blob/master/LICENSE.txt |
| 163 | + * |
| 164 | + * Unless required by applicable law or agreed to in writing, software |
| 165 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 166 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 167 | + * |
| 168 | + * See the License for the specific language governing permissions and |
| 169 | + * limitations under the License. |
| 170 | + * |
| 171 | + *****************************************************************************/ |
0 commit comments