Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee914df

Browse files
authored
EF Core JSON column sample (#341)
* README update for JSON column sample * Create json-columns sample * Rename json-columns to json-columns.cs Added file extension. * Rename samples/ef-core/json-columns.cs to samples/ef-core/json/json-columns.cs
1 parent 3c2e1ad commit ee914df

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

‎samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Entity Framework Core
107107
* Autonomous Database Sample: Demonstrates Oracle EF Core connecting to Oracle Autonomous Database. <br>
108108
* Dependency Injection Sample: Demonstrates using dependency injection and ASP.NET Core with Oracle EF Core. <br>
109109
* Getting Started Sample: Demonstrates a basic Oracle EF Core scenario using migrations and scaffolding. <br>
110+
* JSON Columns Sample: Demonstrates how to create an owned entity, insert, queriy, updates and delete JSON column data. <br>
110111
* Keyless Entity Types Sample: Demonstrates Oracle EF Core keyless entity types with relational and materialized views. <br>
111112
* Stored Procedure Result Set Samples: Demonstrates using PL/SQL that returns either an explicitly or implicitly bound REF Cursor.
112113

‎samples/ef-core/json/json-columns.cs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /