From d6b5a9507b0724e7d8091931afc22cb7fcf25e8c Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 20 Jun 2022 12:42:35 +0500 Subject: [PATCH] Add explicitly props conversion to json for other dbProviders. --- AsbCloudDb/EFExtentions.cs | 22 ++++++++ AsbCloudDb/Model/AsbCloudDbContext.cs | 76 ++++++++++++++++++++++----- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/AsbCloudDb/EFExtentions.cs b/AsbCloudDb/EFExtentions.cs index 596787b5..6f6dac34 100644 --- a/AsbCloudDb/EFExtentions.cs +++ b/AsbCloudDb/EFExtentions.cs @@ -11,6 +11,28 @@ namespace AsbCloudDb { public static class EFExtentions { + private static System.Text.Json.JsonSerializerOptions jsonSerializerOptions = new() + { + AllowTrailingCommas = true, + WriteIndented = true, + NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString | + System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals, + }; + + public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder HasJsonConversion( + this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder builder) + => HasJsonConversion(builder, jsonSerializerOptions); + + public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder HasJsonConversion( + this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder builder, + System.Text.Json.JsonSerializerOptions jsonSerializerOptions) + { + builder.HasConversion( + s => System.Text.Json.JsonSerializer.Serialize(s, jsonSerializerOptions), + s => System.Text.Json.JsonSerializer.Deserialize(s, jsonSerializerOptions)!); + return builder; + } + static Dictionary QueryFactories { get; set; } = new(); static QueryStringFactory GetQueryStringFactory(DbSet dbSet) diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index e1f1d6b8..895f1a00 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -56,6 +56,14 @@ namespace AsbCloudDb.Model public DbSet Record60 => Set(); public DbSet Record61 => Set(); + private System.Text.Json.JsonSerializerOptions jsonSerializerOptions = new() + { + AllowTrailingCommas = true, + WriteIndented = true, + NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString | + System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals, + }; + public AsbCloudDbContext() : base() { } @@ -76,12 +84,47 @@ namespace AsbCloudDb.Model modelBuilder.HasPostgresExtension("adminpack") .HasAnnotation("Relational:Collation", "Russian_Russia.1251"); + modelBuilder.Entity(entity => + { + entity.Property(e => e.Timezone) + .HasJsonConversion(); + }); + modelBuilder.Entity(entity => { entity.HasOne(d => d.Deposit) .WithMany(p => p.Clusters) .HasForeignKey(d => d.IdDeposit) .HasConstraintName("t_cluster_t_deposit_id_fk"); + + entity.Property(e => e.Timezone) + .HasJsonConversion(); + }); + + modelBuilder.Entity(entity => + { + entity.HasOne(d => d.Cluster) + .WithMany(p => p.Wells) + .HasForeignKey(d => d.IdCluster) + .HasConstraintName("t_well_t_cluster_id_fk"); + + entity.HasOne(d => d.Telemetry) + .WithOne(p => p.Well) + .HasForeignKey(d => d.IdTelemetry) + .OnDelete(DeleteBehavior.SetNull) + .HasConstraintName("t_well_t_telemetry_id_fk"); + + entity.Property(e => e.Timezone) + .HasJsonConversion(); + }); + + modelBuilder.Entity(entity => + { + entity.Property(e => e.Info) + .HasJsonConversion(); + + entity.Property(e => e.TimeZone) + .HasJsonConversion(); }); modelBuilder.Entity(entity => @@ -175,20 +218,6 @@ namespace AsbCloudDb.Model .IsUnique(); }); - modelBuilder.Entity(entity => - { - entity.HasOne(d => d.Cluster) - .WithMany(p => p.Wells) - .HasForeignKey(d => d.IdCluster) - .HasConstraintName("t_well_t_cluster_id_fk"); - - entity.HasOne(d => d.Telemetry) - .WithOne(p => p.Well) - .HasForeignKey(d => d.IdTelemetry) - .OnDelete(DeleteBehavior.SetNull) - .HasConstraintName("t_well_t_telemetry_id_fk"); - }); - modelBuilder.Entity(entity => { entity.HasKey(nameof(RelationCompanyWell.IdCompany), nameof(RelationCompanyWell.IdWell)); @@ -241,6 +270,18 @@ namespace AsbCloudDb.Model .IsUnique(); }); + modelBuilder.Entity(entity => + { + entity.Property(e => e.Data) + .HasJsonConversion(); + }); + + modelBuilder.Entity(entity => + { + entity.Property(e => e.PublishInfo) + .HasJsonConversion(); + }); + modelBuilder.Entity(entity => { entity.HasOne(d => d.User) @@ -278,6 +319,8 @@ namespace AsbCloudDb.Model { entity.HasKey(e => new { e.IdWell, e.StartDate }) .HasName("t_id_well_date_start_pk"); + entity.Property(e => e.Info) + .HasJsonConversion(); }); modelBuilder.Entity(entity => @@ -294,6 +337,11 @@ namespace AsbCloudDb.Model .HasConstraintName("t_schedule_t_driller_id_driller"); }); + modelBuilder.Entity(entity => { + entity.Property(e => e.Setpoints) + .HasJsonConversion(); + }); + FillData(modelBuilder); }