diff --git a/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRouteMatch.cs b/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRouteMatch.cs index 69a5704..cb01eea 100644 --- a/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRouteMatch.cs +++ b/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRouteMatch.cs @@ -1,3 +1,7 @@ +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; + namespace Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; /// @@ -24,11 +28,15 @@ public class GwRouteMatch /// /// Header 匹配规则 /// + [NotMapped] + [JsonInclude] public List? Headers { get; set; } /// /// 查询参数匹配规则 /// + [NotMapped] + [JsonInclude] public List? QueryParameters { get; set; } } diff --git a/Fengling.Platform.Infrastructure/PlatformDbContext.cs b/Fengling.Platform.Infrastructure/PlatformDbContext.cs index b5b71bc..75a8095 100644 --- a/Fengling.Platform.Infrastructure/PlatformDbContext.cs +++ b/Fengling.Platform.Infrastructure/PlatformDbContext.cs @@ -4,6 +4,9 @@ using Fengling.Platform.Domain.AggregatesModel.TenantAggregate; using Fengling.Platform.Domain.AggregatesModel.UserAggregate; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using System.Text.Json; namespace Fengling.Platform.Infrastructure; @@ -25,6 +28,12 @@ public class PlatformDbContext(DbContextOptions options) throw new ArgumentNullException(nameof(modelBuilder)); } + // 忽略这些类型,让它们只作为 JSON 值对象使用 + modelBuilder.Ignore(); + modelBuilder.Ignore(); + modelBuilder.Ignore(); + modelBuilder.Ignore(); + modelBuilder.Entity(entity => { entity.Property(e => e.PhoneNumber).HasMaxLength(20); @@ -100,17 +109,28 @@ public class PlatformDbContext(DbContextOptions options) ) .HasMaxLength(50); - // 值对象映射为 JSON 列 - entity.OwnsOne(e => e.Match, navigationBuilder => - { - navigationBuilder.ToJson(); - }); + // 值对象映射为 JSON 列 - 使用值转换器 + var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + entity.Property(e => e.Match) + .HasConversion( + v => JsonSerializer.Serialize(v, jsonOptions), + v => JsonSerializer.Deserialize(v, jsonOptions)!, + new ValueComparer( + (c1, c2) => JsonSerializer.Serialize(c1, jsonOptions) == JsonSerializer.Serialize(c2, jsonOptions), + c => c == null ? 0 : JsonSerializer.Serialize(c, jsonOptions).GetHashCode(), + c => JsonSerializer.Deserialize(JsonSerializer.Serialize(c, jsonOptions), jsonOptions)!)) + .HasColumnType("jsonb"); - // 转换规则映射为 JSON 列 - entity.OwnsMany(e => e.Transforms, navigationBuilder => - { - navigationBuilder.ToJson(); - }); + // 转换规则映射为 JSON 列 - 使用值转换器 + entity.Property(e => e.Transforms) + .HasConversion( + v => JsonSerializer.Serialize(v, jsonOptions), + v => JsonSerializer.Deserialize>(v, jsonOptions), + new ValueComparer>( + (c1, c2) => JsonSerializer.Serialize(c1, jsonOptions) == JsonSerializer.Serialize(c2, jsonOptions), + c => c == null ? 0 : JsonSerializer.Serialize(c, jsonOptions).GetHashCode(), + c => JsonSerializer.Deserialize>(JsonSerializer.Serialize(c, jsonOptions), jsonOptions)!)) + .HasColumnType("jsonb"); entity.HasIndex(e => e.TenantCode); entity.HasIndex(e => e.ServiceName);