-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3c3a17
commit f571d1e
Showing
26 changed files
with
3,653 additions
and
404 deletions.
There are no files selected for viewing
1,879 changes: 1,618 additions & 261 deletions
1,879
entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md
Large diffs are not rendered by default.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
samples/core/Miscellaneous/CompiledModels/MultipleRuntimeModels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using CompiledModelTest; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
|
||
#pragma warning disable 219, 612, 618 | ||
#nullable disable | ||
|
||
namespace MultipleRuntimeModels | ||
{ | ||
#region RuntimeModelCache | ||
public static class RuntimeModelCache | ||
{ | ||
private static readonly ConcurrentDictionary<string, IModel> _runtimeModels | ||
= new(); | ||
|
||
public static IModel GetOrCreateModel(string connectionString) | ||
=> _runtimeModels.GetOrAdd( | ||
connectionString, cs => | ||
{ | ||
if (cs.Contains("X")) | ||
{ | ||
return BlogsContextModel1.Instance; | ||
} | ||
if (cs.Contains("Y")) | ||
{ | ||
return BlogsContextModel2.Instance; | ||
} | ||
throw new InvalidOperationException("No appropriate compiled model found."); | ||
}); | ||
} | ||
#endregion | ||
|
||
[DbContext(typeof(BlogsContext))] | ||
partial class BlogsContextModel1 : RuntimeModel | ||
{ | ||
private static BlogsContextModel1 _instance; | ||
public static IModel Instance | ||
{ | ||
get | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new BlogsContextModel1(); | ||
_instance.Initialize(); | ||
_instance.Customize(); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
|
||
partial void Initialize(); | ||
|
||
partial void Customize(); | ||
} | ||
|
||
[DbContext(typeof(BlogsContext))] | ||
partial class BlogsContextModel2 : RuntimeModel | ||
{ | ||
private static BlogsContextModel2 _instance; | ||
public static IModel Instance | ||
{ | ||
get | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new BlogsContextModel2(); | ||
_instance.Initialize(); | ||
_instance.Customize(); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
|
||
partial void Initialize(); | ||
|
||
partial void Customize(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
samples/core/Miscellaneous/CompiledModels/SingleRuntimeModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using CompiledModelTest; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
|
||
#pragma warning disable 219, 612, 618 | ||
#nullable disable | ||
|
||
namespace SingleRuntimeModel | ||
{ | ||
#region RuntimeModel | ||
[DbContext(typeof(BlogsContext))] | ||
partial class BlogsContextModel : RuntimeModel | ||
{ | ||
private static BlogsContextModel _instance; | ||
public static IModel Instance | ||
{ | ||
get | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new BlogsContextModel(); | ||
_instance.Initialize(); | ||
_instance.Customize(); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
|
||
partial void Initialize(); | ||
|
||
partial void Customize(); | ||
} | ||
#endregion | ||
} |
197 changes: 197 additions & 0 deletions
197
samples/core/Miscellaneous/NewInEFCore6.Cosmos/CosmosImplicitOwnershipSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
public static class CosmosImplicitOwnershipSample | ||
{ | ||
public static void Cosmos_models_use_implicit_ownership_by_default() | ||
{ | ||
Console.WriteLine($">>>> Sample: {nameof(Cosmos_models_use_implicit_ownership_by_default)}"); | ||
Console.WriteLine(); | ||
|
||
using (var context = new FamilyContext()) | ||
{ | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
context.AddRange( | ||
new Family | ||
{ | ||
Id = "Andersen.1", | ||
LastName = "Andersen", | ||
Parents = | ||
{ | ||
new() { FirstName = "Thomas" }, new() { FirstName = "Mary Kay" } | ||
}, | ||
Children = | ||
{ | ||
new() | ||
{ | ||
FirstName = "Henriette Thaulow", | ||
Gender = "female", | ||
Grade = 5, | ||
Pets = { new() { GivenName = "Fluffy" } } | ||
} | ||
}, | ||
Address = new Address { State = "WA", County = "King", City = "Seattle" }, | ||
}, | ||
new Family | ||
{ | ||
Id = "Wakefield.7", | ||
LastName = "Wakefield", | ||
Parents = | ||
{ | ||
new() { FamilyName = "Wakefield", FirstName = "Robin" }, | ||
new() { FamilyName = "Miller", FirstName = "Ben" } | ||
}, | ||
Children = | ||
{ | ||
new() | ||
{ | ||
FamilyName = "Merriam", | ||
FirstName = "Jesse", | ||
Gender = "female", | ||
Grade = 8, | ||
Pets = { new() { GivenName = "Goofy" }, new() { GivenName = "Shadow" } } | ||
}, | ||
new Child | ||
{ | ||
FamilyName = "Miller", | ||
FirstName = "Lisa", | ||
Gender = "female", | ||
Grade = 1 | ||
} | ||
}, | ||
Address = new Address { State = "NY", County = "Manhattan", City = "NY" }, | ||
IsRegistered = true | ||
}); | ||
|
||
context.SaveChanges(); | ||
} | ||
|
||
Console.WriteLine(); | ||
|
||
using (var context = new FamilyContext()) | ||
{ | ||
var families = context.Families.ToList(); | ||
|
||
Console.WriteLine(); | ||
|
||
foreach (var family in families) | ||
{ | ||
Console.WriteLine($"{family.LastName} family:"); | ||
Console.WriteLine($" From {family.Address.City}, {family.Address.State}"); | ||
Console.WriteLine($" With {family.Children.Count} children and {family.Children.SelectMany(e => e.Pets).Count()} pets."); | ||
} | ||
} | ||
|
||
Console.WriteLine(); | ||
} | ||
|
||
#region Model | ||
public class Family | ||
{ | ||
[JsonPropertyName("id")] | ||
public string Id { get; set; } | ||
|
||
public string LastName { get; set; } | ||
public bool IsRegistered { get; set; } | ||
|
||
public Address Address { get; set; } | ||
|
||
public IList<Parent> Parents { get; } = new List<Parent>(); | ||
public IList<Child> Children { get; } = new List<Child>(); | ||
} | ||
|
||
public class Parent | ||
{ | ||
public string FamilyName { get; set; } | ||
public string FirstName { get; set; } | ||
} | ||
|
||
public class Child | ||
{ | ||
public string FamilyName { get; set; } | ||
public string FirstName { get; set; } | ||
public int Grade { get; set; } | ||
|
||
public string Gender { get; set; } | ||
|
||
public IList<Pet> Pets { get; } = new List<Pet>(); | ||
} | ||
#endregion | ||
|
||
public class Pet | ||
{ | ||
public string GivenName { get; set; } | ||
} | ||
|
||
public class Address | ||
{ | ||
public string State { get; set; } | ||
public string County { get; set; } | ||
public string City { get; set; } | ||
} | ||
|
||
public class FamilyContext : DbContext | ||
{ | ||
public DbSet<Family> Families { get; set; } | ||
|
||
private readonly bool _quiet; | ||
|
||
public FamilyContext(bool quiet = false) | ||
{ | ||
_quiet = quiet; | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
#region OnModelCreating | ||
modelBuilder.Entity<Family>().HasPartitionKey(e => e.LastName); | ||
#endregion | ||
} | ||
|
||
// Never called; just for documentation. | ||
private void OldOnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
#region OldOnModelCreating | ||
modelBuilder.Entity<Family>() | ||
.HasPartitionKey(e => e.LastName) | ||
.OwnsMany(f => f.Parents); | ||
|
||
modelBuilder.Entity<Family>() | ||
.OwnsMany(f => f.Children) | ||
.OwnsMany(c => c.Pets); | ||
|
||
modelBuilder.Entity<Family>() | ||
.OwnsOne(f => f.Address); | ||
#endregion | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder | ||
.EnableSensitiveDataLogging() | ||
.UseCosmos( | ||
"https://localhost:8081", | ||
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", | ||
"TODOImplicit"); | ||
|
||
if (!_quiet) | ||
{ | ||
optionsBuilder.LogTo( | ||
Console.WriteLine, new[] | ||
{ | ||
CosmosEventId.ExecutedCreateItem, | ||
CosmosEventId.ExecutedDeleteItem, | ||
CosmosEventId.ExecutedReadItem, | ||
CosmosEventId.ExecutedReadNext, | ||
CosmosEventId.ExecutedReplaceItem, | ||
}); | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
samples/core/Miscellaneous/NewInEFCore6.Cosmos/CosmosMinimalApiSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class CosmosMinimalApiSample | ||
{ | ||
public static void Add_a_DbContext_and_provider() | ||
{ | ||
Console.WriteLine($">>>> Sample: {nameof(Add_a_DbContext_and_provider)}"); | ||
|
||
CosmosMinimal(null); | ||
CosmosNormal(null); | ||
} | ||
|
||
private static void CosmosMinimal(string[] args) | ||
{ | ||
#region CosmosMinimal | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddCosmos<MyDbContext>( | ||
"https://localhost:8081", | ||
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="); | ||
#endregion | ||
} | ||
|
||
private static void CosmosNormal(string[] args) | ||
{ | ||
#region CosmosNormal | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddDbContext<MyDbContext>( | ||
options => options.UseCosmos( | ||
"https://localhost:8081", | ||
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==")); | ||
#endregion | ||
} | ||
|
||
// This is a fake implementation of WebApplicationBuilder uses to show the EF Core minimal APIs | ||
private class FakeWebApplicationBuilder | ||
{ | ||
public ServiceCollection Services { get; } = new(); | ||
} | ||
|
||
// This is a fake implementation of WebApplication uses to show the EF Core minimal APIs | ||
private static class WebApplication | ||
{ | ||
public static FakeWebApplicationBuilder CreateBuilder(string[] args) | ||
=> new(); | ||
} | ||
|
||
public class MyDbContext : DbContext | ||
{ | ||
public MyDbContext(DbContextOptions<MyDbContext> options) | ||
: base(options) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.