|
1 | | -using System.Text.Json; |
2 | | -using Microsoft.EntityFrameworkCore; |
3 | | -using Microsoft.EntityFrameworkCore.Metadata.Builders; |
4 | | - |
5 | | -namespace SIL.Harmony.Db.EntityConfig; |
6 | | - |
7 | | -public class CommitEntityConfig : IEntityTypeConfiguration<Commit> |
8 | | -{ |
9 | | - public void Configure(EntityTypeBuilder<Commit> builder) |
10 | | - { |
11 | | - builder.ToTable("Commits"); |
12 | | - builder.HasKey(c => c.Id); |
13 | | - builder.ComplexProperty(c => c.HybridDateTime, |
14 | | - hybridEntity => |
15 | | - { |
16 | | - hybridEntity.Property(h => h.DateTime) |
17 | | - .HasConversion( |
18 | | - d => d.UtcDateTime, |
19 | | - //need to use ticks here because the DateTime is stored as UTC, but the db records it as unspecified |
20 | | - d => new DateTimeOffset(d.Ticks, TimeSpan.Zero)) |
21 | | - .HasColumnName("DateTime"); |
22 | | - hybridEntity.Property(h => h.Counter).HasColumnName("Counter"); |
23 | | - }); |
24 | | - builder.Property(c => c.Metadata) |
25 | | - .HasColumnType("jsonb") |
26 | | - .HasConversion( |
27 | | - m => JsonSerializer.Serialize(m, (JsonSerializerOptions?)null), |
28 | | - json => JsonSerializer.Deserialize<CommitMetadata>(json, (JsonSerializerOptions?)null) ?? new() |
29 | | - ); |
30 | | - builder.HasMany(c => c.ChangeEntities) |
31 | | - .WithOne() |
32 | | - .HasForeignKey(c => c.CommitId); |
33 | | - } |
34 | | -} |
| 1 | +using System.Text.Json; |
| 2 | +using EFCore.ComplexIndexes; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 5 | + |
| 6 | +namespace SIL.Harmony.Db.EntityConfig; |
| 7 | + |
| 8 | +public class CommitEntityConfig : IEntityTypeConfiguration<Commit> |
| 9 | +{ |
| 10 | + public void Configure(EntityTypeBuilder<Commit> builder) |
| 11 | + { |
| 12 | + builder.ToTable("Commits"); |
| 13 | + builder.HasKey(c => c.Id); |
| 14 | + builder.ComplexProperty(c => c.HybridDateTime, |
| 15 | + hybridEntity => |
| 16 | + { |
| 17 | + hybridEntity.Property(h => h.DateTime) |
| 18 | + .HasConversion( |
| 19 | + d => d.UtcDateTime, |
| 20 | + //need to use ticks here because the DateTime is stored as UTC, but the db records it as unspecified |
| 21 | + d => new DateTimeOffset(d.Ticks, TimeSpan.Zero)) |
| 22 | + .HasColumnName("DateTime"); |
| 23 | + hybridEntity.Property(h => h.Counter).HasColumnName("Counter"); |
| 24 | + }); |
| 25 | + // Supports Harmony's default ordering (DateTime DESC, Counter DESC, Id DESC). |
| 26 | + // EF Core 10 cannot express indexes mixing ComplexProperty members + scalars (efcore#11336, targeted for 11). |
| 27 | + // We use EFCore.ComplexIndexes instead. The package doesn't support column direction, |
| 28 | + // but an ASC index apparently works equivalently for reverse scans on SQLite, Postgres, and SQL Server. |
| 29 | + builder.HasComplexCompositeIndex( |
| 30 | + c => new { c.HybridDateTime.DateTime, c.HybridDateTime.Counter, c.Id }, |
| 31 | + indexName: "IX_Commits_DateTime_Counter_Id"); |
| 32 | + builder.Property(c => c.Metadata) |
| 33 | + .HasColumnType("jsonb") |
| 34 | + .HasConversion( |
| 35 | + m => JsonSerializer.Serialize(m, (JsonSerializerOptions?)null), |
| 36 | + json => JsonSerializer.Deserialize<CommitMetadata>(json, (JsonSerializerOptions?)null) ?? new() |
| 37 | + ); |
| 38 | + builder.HasMany(c => c.ChangeEntities) |
| 39 | + .WithOne() |
| 40 | + .HasForeignKey(c => c.CommitId); |
| 41 | + } |
| 42 | +} |
0 commit comments