-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Cosmos prerender: single container with $type discriminator #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace ExpressiveSharp.Docs.PlaygroundModel.Webshop; | ||
|
|
||
| // Shared base so Cosmos can host all 4 types in a single container, accessed | ||
| // as `DbSet<WebshopEntity>.OfType<T>()`. `Id` lives here because EF Core's TPH | ||
| // discriminator model requires the key on the root type. | ||
| public abstract class WebshopEntity | ||
| { | ||
| public int Id { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using ExpressiveSharp.Docs.PlaygroundModel.Webshop; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace ExpressiveSharp.Docs.Prerenderer; | ||
|
|
||
| // Single shared Cosmos container; every entity lives in it and is | ||
| // discriminated by $type. Samples access each concrete type via | ||
| // `DbSet<WebshopEntity>.OfType<T>()`, so every query stays rooted at | ||
| // one entity type from Cosmos's perspective — sidestepping the | ||
| // "Root entity X already being referenced" failure. | ||
| internal sealed class WebshopCosmosDbContext : WebshopDbContext | ||
| { | ||
| public WebshopCosmosDbContext(DbContextOptions<WebshopDbContext> options) : base(options) { } | ||
|
|
||
| public DbSet<WebshopEntity> Entities => Set<WebshopEntity>(); | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder b) | ||
| { | ||
| b.Entity<WebshopEntity>(e => | ||
| { | ||
| e.ToContainer("webshop"); | ||
| e.HasKey(x => x.Id); | ||
| e.HasPartitionKey(x => x.Id); | ||
| e.HasDiscriminator<string>("$type") | ||
| .HasValue<Customer>("Customer") | ||
| .HasValue<Order>("Order") | ||
| .HasValue<Product>("Product") | ||
| .HasValue<LineItem>("LineItem"); | ||
| }); | ||
|
|
||
| b.Entity<Order>(e => e.Ignore(o => o.Customer)); | ||
|
|
||
| b.Entity<LineItem>(e => | ||
| { | ||
| e.Ignore(i => i.Order); | ||
| e.Ignore(i => i.Product); | ||
|
Comment on lines
+31
to
+36
|
||
| e.Property(i => i.UnitPrice).HasPrecision(18, 2); | ||
| }); | ||
|
|
||
| b.Entity<Product>(e => e.Property(p => p.ListPrice).HasPrecision(18, 2)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,6 +399,11 @@ public static IExpressiveQueryable<T> DefaultIfEmpty<T>( | |
| => Queryable.Index(source).AsExpressive(); | ||
| #endif | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public static IExpressiveQueryable<TResult> OfType<T, TResult>( | ||
| this IExpressiveQueryable<T> source) | ||
| => Queryable.OfType<TResult>(source).AsExpressive(); | ||
|
Comment on lines
+402
to
+405
|
||
|
|
||
| // ── Non-lambda-first intercepted methods ───────────────────────────── | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment mentions filtering via
OfType<T>(), but the actual call site uses the newOfType<T, TResult>()overload (requiring two generic arguments). Tweaking the comment to match the actual API shape would make this easier to follow for future maintainers.