Skip to content

Commit

Permalink
Remaining What's New in EF Core 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Sep 28, 2021
1 parent f3c3a17 commit 0c18334
Show file tree
Hide file tree
Showing 27 changed files with 3,654 additions and 405 deletions.
1,879 changes: 1,618 additions & 261 deletions entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions samples/core/Miscellaneous/CompiledModels/MultipleRuntimeModels.cs
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 samples/core/Miscellaneous/CompiledModels/SingleRuntimeModel.cs
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
}
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,
});
}
}
}
}
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)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Address
public string County { get; set; }
public string City { get; set; }
}

public class FamilyContext : DbContext
{
public DbSet<Family> Families { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions samples/core/Miscellaneous/NewInEFCore6.Cosmos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public static void Main()
CosmosQueriesSample.Cosmos_queries();
CosmosDiagnosticsSample.Cosmos_diagnostics();
CosmosModelConfigurationSample.Cosmos_configure_time_to_live();
CosmosImplicitOwnershipSample.Cosmos_models_use_implicit_ownership_by_default();
CosmosMinimalApiSample.Add_a_DbContext_and_provider();
}
}
Loading

0 comments on commit 0c18334

Please sign in to comment.