Skip to content

Commit 0c18334

Browse files
committed
Remaining What's New in EF Core 6.0
1 parent f3c3a17 commit 0c18334

27 files changed

+3654
-405
lines changed

entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md

Lines changed: 1618 additions & 261 deletions
Large diffs are not rendered by default.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using CompiledModelTest;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
7+
#pragma warning disable 219, 612, 618
8+
#nullable disable
9+
10+
namespace MultipleRuntimeModels
11+
{
12+
#region RuntimeModelCache
13+
public static class RuntimeModelCache
14+
{
15+
private static readonly ConcurrentDictionary<string, IModel> _runtimeModels
16+
= new();
17+
18+
public static IModel GetOrCreateModel(string connectionString)
19+
=> _runtimeModels.GetOrAdd(
20+
connectionString, cs =>
21+
{
22+
if (cs.Contains("X"))
23+
{
24+
return BlogsContextModel1.Instance;
25+
}
26+
27+
if (cs.Contains("Y"))
28+
{
29+
return BlogsContextModel2.Instance;
30+
}
31+
32+
throw new InvalidOperationException("No appropriate compiled model found.");
33+
});
34+
}
35+
#endregion
36+
37+
[DbContext(typeof(BlogsContext))]
38+
partial class BlogsContextModel1 : RuntimeModel
39+
{
40+
private static BlogsContextModel1 _instance;
41+
public static IModel Instance
42+
{
43+
get
44+
{
45+
if (_instance == null)
46+
{
47+
_instance = new BlogsContextModel1();
48+
_instance.Initialize();
49+
_instance.Customize();
50+
}
51+
52+
return _instance;
53+
}
54+
}
55+
56+
partial void Initialize();
57+
58+
partial void Customize();
59+
}
60+
61+
[DbContext(typeof(BlogsContext))]
62+
partial class BlogsContextModel2 : RuntimeModel
63+
{
64+
private static BlogsContextModel2 _instance;
65+
public static IModel Instance
66+
{
67+
get
68+
{
69+
if (_instance == null)
70+
{
71+
_instance = new BlogsContextModel2();
72+
_instance.Initialize();
73+
_instance.Customize();
74+
}
75+
76+
return _instance;
77+
}
78+
}
79+
80+
partial void Initialize();
81+
82+
partial void Customize();
83+
}
84+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using CompiledModelTest;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
3+
using Microsoft.EntityFrameworkCore.Metadata;
4+
5+
#pragma warning disable 219, 612, 618
6+
#nullable disable
7+
8+
namespace SingleRuntimeModel
9+
{
10+
#region RuntimeModel
11+
[DbContext(typeof(BlogsContext))]
12+
partial class BlogsContextModel : RuntimeModel
13+
{
14+
private static BlogsContextModel _instance;
15+
public static IModel Instance
16+
{
17+
get
18+
{
19+
if (_instance == null)
20+
{
21+
_instance = new BlogsContextModel();
22+
_instance.Initialize();
23+
_instance.Customize();
24+
}
25+
26+
return _instance;
27+
}
28+
}
29+
30+
partial void Initialize();
31+
32+
partial void Customize();
33+
}
34+
#endregion
35+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.Json.Serialization;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Diagnostics;
7+
8+
public static class CosmosImplicitOwnershipSample
9+
{
10+
public static void Cosmos_models_use_implicit_ownership_by_default()
11+
{
12+
Console.WriteLine($">>>> Sample: {nameof(Cosmos_models_use_implicit_ownership_by_default)}");
13+
Console.WriteLine();
14+
15+
using (var context = new FamilyContext())
16+
{
17+
context.Database.EnsureDeleted();
18+
context.Database.EnsureCreated();
19+
20+
context.AddRange(
21+
new Family
22+
{
23+
Id = "Andersen.1",
24+
LastName = "Andersen",
25+
Parents =
26+
{
27+
new() { FirstName = "Thomas" }, new() { FirstName = "Mary Kay" }
28+
},
29+
Children =
30+
{
31+
new()
32+
{
33+
FirstName = "Henriette Thaulow",
34+
Gender = "female",
35+
Grade = 5,
36+
Pets = { new() { GivenName = "Fluffy" } }
37+
}
38+
},
39+
Address = new Address { State = "WA", County = "King", City = "Seattle" },
40+
},
41+
new Family
42+
{
43+
Id = "Wakefield.7",
44+
LastName = "Wakefield",
45+
Parents =
46+
{
47+
new() { FamilyName = "Wakefield", FirstName = "Robin" },
48+
new() { FamilyName = "Miller", FirstName = "Ben" }
49+
},
50+
Children =
51+
{
52+
new()
53+
{
54+
FamilyName = "Merriam",
55+
FirstName = "Jesse",
56+
Gender = "female",
57+
Grade = 8,
58+
Pets = { new() { GivenName = "Goofy" }, new() { GivenName = "Shadow" } }
59+
},
60+
new Child
61+
{
62+
FamilyName = "Miller",
63+
FirstName = "Lisa",
64+
Gender = "female",
65+
Grade = 1
66+
}
67+
},
68+
Address = new Address { State = "NY", County = "Manhattan", City = "NY" },
69+
IsRegistered = true
70+
});
71+
72+
context.SaveChanges();
73+
}
74+
75+
Console.WriteLine();
76+
77+
using (var context = new FamilyContext())
78+
{
79+
var families = context.Families.ToList();
80+
81+
Console.WriteLine();
82+
83+
foreach (var family in families)
84+
{
85+
Console.WriteLine($"{family.LastName} family:");
86+
Console.WriteLine($" From {family.Address.City}, {family.Address.State}");
87+
Console.WriteLine($" With {family.Children.Count} children and {family.Children.SelectMany(e => e.Pets).Count()} pets.");
88+
}
89+
}
90+
91+
Console.WriteLine();
92+
}
93+
94+
#region Model
95+
public class Family
96+
{
97+
[JsonPropertyName("id")]
98+
public string Id { get; set; }
99+
100+
public string LastName { get; set; }
101+
public bool IsRegistered { get; set; }
102+
103+
public Address Address { get; set; }
104+
105+
public IList<Parent> Parents { get; } = new List<Parent>();
106+
public IList<Child> Children { get; } = new List<Child>();
107+
}
108+
109+
public class Parent
110+
{
111+
public string FamilyName { get; set; }
112+
public string FirstName { get; set; }
113+
}
114+
115+
public class Child
116+
{
117+
public string FamilyName { get; set; }
118+
public string FirstName { get; set; }
119+
public int Grade { get; set; }
120+
121+
public string Gender { get; set; }
122+
123+
public IList<Pet> Pets { get; } = new List<Pet>();
124+
}
125+
#endregion
126+
127+
public class Pet
128+
{
129+
public string GivenName { get; set; }
130+
}
131+
132+
public class Address
133+
{
134+
public string State { get; set; }
135+
public string County { get; set; }
136+
public string City { get; set; }
137+
}
138+
139+
public class FamilyContext : DbContext
140+
{
141+
public DbSet<Family> Families { get; set; }
142+
143+
private readonly bool _quiet;
144+
145+
public FamilyContext(bool quiet = false)
146+
{
147+
_quiet = quiet;
148+
}
149+
150+
protected override void OnModelCreating(ModelBuilder modelBuilder)
151+
{
152+
#region OnModelCreating
153+
modelBuilder.Entity<Family>().HasPartitionKey(e => e.LastName);
154+
#endregion
155+
}
156+
157+
// Never called; just for documentation.
158+
private void OldOnModelCreating(ModelBuilder modelBuilder)
159+
{
160+
#region OldOnModelCreating
161+
modelBuilder.Entity<Family>()
162+
.HasPartitionKey(e => e.LastName)
163+
.OwnsMany(f => f.Parents);
164+
165+
modelBuilder.Entity<Family>()
166+
.OwnsMany(f => f.Children)
167+
.OwnsMany(c => c.Pets);
168+
169+
modelBuilder.Entity<Family>()
170+
.OwnsOne(f => f.Address);
171+
#endregion
172+
}
173+
174+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
175+
{
176+
optionsBuilder
177+
.EnableSensitiveDataLogging()
178+
.UseCosmos(
179+
"https://localhost:8081",
180+
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
181+
"TODOImplicit");
182+
183+
if (!_quiet)
184+
{
185+
optionsBuilder.LogTo(
186+
Console.WriteLine, new[]
187+
{
188+
CosmosEventId.ExecutedCreateItem,
189+
CosmosEventId.ExecutedDeleteItem,
190+
CosmosEventId.ExecutedReadItem,
191+
CosmosEventId.ExecutedReadNext,
192+
CosmosEventId.ExecutedReplaceItem,
193+
});
194+
}
195+
}
196+
}
197+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
public static class CosmosMinimalApiSample
6+
{
7+
public static void Add_a_DbContext_and_provider()
8+
{
9+
Console.WriteLine($">>>> Sample: {nameof(Add_a_DbContext_and_provider)}");
10+
11+
CosmosMinimal(null);
12+
CosmosNormal(null);
13+
}
14+
15+
private static void CosmosMinimal(string[] args)
16+
{
17+
#region CosmosMinimal
18+
var builder = WebApplication.CreateBuilder(args);
19+
20+
builder.Services.AddCosmos<MyDbContext>(
21+
"https://localhost:8081",
22+
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
23+
#endregion
24+
}
25+
26+
private static void CosmosNormal(string[] args)
27+
{
28+
#region CosmosNormal
29+
var builder = WebApplication.CreateBuilder(args);
30+
31+
builder.Services.AddDbContext<MyDbContext>(
32+
options => options.UseCosmos(
33+
"https://localhost:8081",
34+
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="));
35+
#endregion
36+
}
37+
38+
// This is a fake implementation of WebApplicationBuilder uses to show the EF Core minimal APIs
39+
private class FakeWebApplicationBuilder
40+
{
41+
public ServiceCollection Services { get; } = new();
42+
}
43+
44+
// This is a fake implementation of WebApplication uses to show the EF Core minimal APIs
45+
private static class WebApplication
46+
{
47+
public static FakeWebApplicationBuilder CreateBuilder(string[] args)
48+
=> new();
49+
}
50+
51+
public class MyDbContext : DbContext
52+
{
53+
public MyDbContext(DbContextOptions<MyDbContext> options)
54+
: base(options)
55+
{
56+
}
57+
}
58+
}

samples/core/Miscellaneous/NewInEFCore6.Cosmos/CosmosModelConfigurationSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class Address
5454
public string County { get; set; }
5555
public string City { get; set; }
5656
}
57+
5758
public class FamilyContext : DbContext
5859
{
5960
public DbSet<Family> Families { get; set; }

samples/core/Miscellaneous/NewInEFCore6.Cosmos/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public static void Main()
77
CosmosQueriesSample.Cosmos_queries();
88
CosmosDiagnosticsSample.Cosmos_diagnostics();
99
CosmosModelConfigurationSample.Cosmos_configure_time_to_live();
10+
CosmosImplicitOwnershipSample.Cosmos_models_use_implicit_ownership_by_default();
11+
CosmosMinimalApiSample.Add_a_DbContext_and_provider();
1012
}
1113
}

0 commit comments

Comments
 (0)