using AsbCloudDb.Model.GTR; using Microsoft.EntityFrameworkCore; using System.Threading; using System.Threading.Tasks; using AsbCloudDb.Model.DailyReports; using AsbCloudDb.Model.Manuals; using AsbCloudDb.Model.ProcessMaps; using AsbCloudDb.Model.WellSections; using AsbCloudDb.Model.Trajectory; namespace AsbCloudDb.Model { public partial class AsbCloudDbContext : DbContext, IAsbCloudDbContext { public virtual DbSet Clusters => Set(); public virtual DbSet Companies => Set(); public virtual DbSet CompaniesTypes => Set(); public virtual DbSet DailyReports => Set(); public virtual DbSet Deposits => Set(); public virtual DbSet DetectedOperations => Set(); public virtual DbSet TrajectoriesPlan => Set(); public virtual DbSet ProcessMapPlanRotor => Set(); public virtual DbSet ProcessMapPlanSlide => Set(); public virtual DbSet ProcessMapPlanReams => Set(); public virtual DbSet ProcessMapPlanOperationReamingRotor => Set(); public virtual DbSet ProcessMapPlanOperationReamingSlide => Set(); public virtual DbSet ProcessMapPlanOperationLoadCapacity => Set(); public virtual DbSet ProcessMapPlanSurvey => Set(); public virtual DbSet ProcessMapPlanOperationPositioningOffTheBottom => Set(); public virtual DbSet ProcessMapPlanOperationDeterminationOfOscillationAngles => Set(); public virtual DbSet ProcessMapPlanOperationTFOrientation => Set(); public virtual DbSet ProcessMapPlanOperationSwitchPump => Set(); public virtual DbSet ProcessMapPlanOperationSwitchMode => Set(); public virtual DbSet ProcessMapPlanFunctionsDrillTest => Set(); public virtual DbSet ProcessMapPlanFunctionsShockTest => Set(); public virtual DbSet ProcessMapPlanFunctionsDamper => Set(); public virtual DbSet ProcessMapPlanFunctionsAutoHold => Set(); public virtual DbSet ProcessMapPlanFunctionsJarrDrillTool => Set(); public virtual DbSet ProcessMapPlanFunctionsUpgradeNoload => Set(); public virtual DbSet ProcessMapPlanFunctionsOscillation => Set(); public virtual DbSet ProcessMapPlanFunctionsAnticrashRotation => Set(); public virtual DbSet ProcessMapPlanFunctionsStaticMeasure => Set(); public virtual DbSet DrillingProgramParts => Set(); public virtual DbSet FileCategories => Set(); public virtual DbSet Files => Set(); public virtual DbSet FileMarks => Set(); public virtual DbSet Measures => Set(); public virtual DbSet MeasureCategories => Set(); public virtual DbSet Permissions => Set(); public virtual DbSet RelationCompaniesWells => Set(); public virtual DbSet RelationDrillingProgramPartUsers => Set(); public virtual DbSet RelationUserRolePermissions => Set(); public virtual DbSet RelationUserRoleUserRoles => Set(); public virtual DbSet RelationUserUserRoles => Set(); public virtual DbSet RelationContactsWells => Set(); public virtual DbSet ReportProperties => Set(); public virtual DbSet SetpointsRequests => Set(); public virtual DbSet Subsystems => Set(); public virtual DbSet Telemetries => Set(); public virtual DbSet TelemetryDataSaub => Set(); public virtual DbSet TelemetryDataSaubStats => Set(); public virtual DbSet TelemetryDataSpin => Set(); public virtual DbSet TelemetryEvents => Set(); public virtual DbSet TelemetryMessages => Set(); public virtual DbSet TelemetryUsers => Set(); public virtual DbSet Users => Set(); public virtual DbSet UserRoles => Set(); public virtual DbSet UserSettings => Set(); public virtual DbSet Wells => Set(); public virtual DbSet WellComposites => Set(); public virtual DbSet WellOperations => Set(); public virtual DbSet WellOperationCategories => Set(); public virtual DbSet WellSectionTypes => Set(); public virtual DbSet WellTypes => Set(); public virtual DbSet Drillers => Set(); public virtual DbSet Schedule => Set(); public virtual DbSet OperationValues => Set(); public virtual DbSet WellFinalDocuments => Set(); public virtual DbSet LimitingParameter => Set(); public virtual DbSet TelemetryWirelineRunOut => Set(); public virtual DbSet TrajectoriesFact => Set(); public virtual DbSet WellSectionsPlan => Set(); // GTR WITS public DbSet WitsItemFloat => Set(); public DbSet WitsItemInt => Set(); public DbSet WitsItemString => Set(); // WITS public DbSet Record1 => Set(); public DbSet Record7 => Set(); public DbSet Record8 => Set(); public DbSet Record50 => Set(); public DbSet Record60 => Set(); public DbSet Record61 => Set(); private static int referenceCount; public static int ReferenceCount => referenceCount; public DbSet Faqs => Set(); public DbSet HelpPages => Set(); public DbSet Notifications => Set(); public DbSet NotificationCategories => Set(); public DbSet Manuals => Set(); public DbSet ManualDirectories => Set(); public DbSet Contacts => Set(); public DbSet DrillTests => Set(); public DbSet DataSaubStat => Set(); public AsbCloudDbContext() : base() { Interlocked.Increment(ref referenceCount); } public AsbCloudDbContext(DbContextOptions options) : base(options) { Interlocked.Increment(ref referenceCount); } ~AsbCloudDbContext() { Interlocked.Decrement(ref referenceCount); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True;" //, builder=>builder.EnableRetryOnFailure(2, System.TimeSpan.FromMinutes(1)) ); } protected override void OnModelCreating(ModelBuilder modelBuilder) { 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 => { entity.HasKey( nameof(WellComposite.IdWell), nameof(WellComposite.IdWellSrc), nameof(WellComposite.IdWellSectionType)); entity.HasOne(d => d.Well) .WithMany(p => p.WellComposites) .HasForeignKey(d => d.IdWell) .HasConstraintName("t_well_сomposite_t_well_id_fk"); entity.HasOne(d => d.WellSrc) .WithMany(p => p.WellCompositeSrcs) .HasForeignKey(d => d.IdWellSrc) .HasConstraintName("t_well_сomposite_src_t_well_id_fk"); entity.HasOne(d => d.WellSectionType) .WithMany(p => p.WellComposites) .HasForeignKey(d => d.IdWellSectionType) .HasConstraintName("t_well_сomposite_t_well_section_type_id_fk"); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.Telemetry) .WithMany(p => p.DataSaub) .HasForeignKey(d => d.IdTelemetry) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_telemetry_data_saub_t_telemetry_id_fk"); entity.HasKey(nameof(ITelemetryData.IdTelemetry), nameof(ITelemetryData.DateTime)); }); modelBuilder.Entity(entity => { entity.HasKey(nameof(ITelemetryData.IdTelemetry), nameof(ITelemetryData.DateTime)); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.Telemetry) .WithMany(p => p.DataSpin) .HasForeignKey(d => d.IdTelemetry) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_telemetry_data_spin_t_telemetry_id_fk"); entity.HasKey(nameof(ITelemetryData.IdTelemetry), nameof(ITelemetryData.DateTime)); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.Telemetry) .WithMany(p => p.Messages) .HasForeignKey(d => d.IdTelemetry) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_messages_t_telemetry_id_fk"); }); modelBuilder.Entity(entity => { entity.HasKey(nameof(TelemetryUser.IdTelemetry), nameof(TelemetryUser.IdUser)); entity.HasOne(d => d.Telemetry) .WithMany(p => p.Users) .HasForeignKey(d => d.IdTelemetry) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_telemetry_user_t_telemetry_id_fk"); }); modelBuilder.Entity(entity => { entity.HasKey(nameof(TelemetryEvent.IdTelemetry), nameof(TelemetryEvent.IdEvent)); entity.HasOne(d => d.Telemetry) .WithMany(p => p.Events) .HasForeignKey(d => d.IdTelemetry) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_event_t_telemetry_id_fk"); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.Company) .WithMany(p => p.Users) .HasForeignKey(d => d.IdCompany) .OnDelete(DeleteBehavior.SetNull) .HasConstraintName("t_user_t_company_id_fk"); entity.HasIndex(d => d.Login) .IsUnique(); }); modelBuilder.Entity(entity => { entity.HasKey(nameof(RelationCompanyWell.IdCompany), nameof(RelationCompanyWell.IdWell)); entity.HasOne(r => r.Well) .WithMany(w => w.RelationCompaniesWells) .HasForeignKey(r => r.IdWell) .HasConstraintName("t_relation_company_well_t_well_id_fk"); entity.HasOne(r => r.Company) .WithMany(w => w.RelationCompaniesWells) .HasForeignKey(r => r.IdCompany) .HasConstraintName("t_relation_company_well_t_company_id_fk"); }); modelBuilder.Entity(entity => { entity.HasKey(x => new { x.Id, x.IdInclude }) .HasName("t_relation_user_role_user_role_pk"); }); modelBuilder.Entity(entity => { entity.HasKey(x => new { x.IdUser, x.IdDrillingProgramPart }) .HasName("t_relation_user_drilling_program_part_pk"); }); modelBuilder.Entity(entity => { entity.HasIndex(d => d.DepthEnd); entity.HasIndex(d => d.DateStart); entity.HasOne(o => o.OperationPlan) .WithMany() .HasForeignKey(o => o.IdPlan) .OnDelete(DeleteBehavior.SetNull); }); modelBuilder.Entity(entity => { entity.HasIndex(x => new { x.IdWell, x.IdFileCategory }) .IsUnique(); }); modelBuilder.Entity(entity => { entity.Property(e => e.Data) .HasJsonConversion(); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.User) .WithMany(p => p.FileMarks) .HasForeignKey(d => d.IdUser) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_user_t_file_mark_fk"); entity.HasOne(d => d.FileInfo) .WithMany(p => p.FileMarks) .HasForeignKey(d => d.IdFile) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("t_file_mark_t_file_info_fk"); }); modelBuilder.Entity(entity => { entity.HasKey(e => new { e.IdUser, e.IdUserRole }); }); modelBuilder.Entity(entity => { entity.HasKey(e => new { e.IdUserRole, e.IdPermission }); entity.HasOne(r => r.Permission) .WithMany(p => p.RelationUserRolePermissions) .IsRequired(); entity.HasOne(r => r.UserRole) .WithMany(r => r.RelationUserRolePermissions) .IsRequired(); }); modelBuilder.Entity(entity => { entity.HasIndex(e => new { e.IdWell, e.Date }) .IsUnique(); entity.Property(e => e.SubsystemBlock) .HasJsonConversion(); entity.Property(e => e.SignBlock) .HasJsonConversion(); entity.Property(e => e.TimeBalanceBlock) .HasJsonConversion(); }); modelBuilder.Entity(entity => entity .Property(p => p.ExtraData) .HasJsonConversion()); modelBuilder.Entity(entity => { entity.HasNoKey() .ToView("mw_telemetry_datas_saub_stat"); }); modelBuilder.Entity(entity => { entity.HasOne(d => d.Driller) .WithMany(p => p.Schedule) .HasForeignKey(d => d.IdDriller) .HasConstraintName("t_schedule_t_driller_id_driller"); }); modelBuilder.Entity(entity => { entity.Property(e => e.Setpoints) .HasJsonConversion(); }); modelBuilder.Entity(entity => { entity.HasKey( nameof(ITelemetryData.IdTelemetry), nameof(WitsItemBase.IdRecord), nameof(WitsItemBase.IdItem), nameof(WitsItemBase.DateTime)); }); modelBuilder.Entity(entity => { entity.HasKey( nameof(ITelemetryData.IdTelemetry), nameof(WitsItemBase.IdRecord), nameof(WitsItemBase.IdItem), nameof(WitsItemBase.DateTime)); }); modelBuilder.Entity(entity => { entity.HasKey( nameof(ITelemetryData.IdTelemetry), nameof(WitsItemBase.IdRecord), nameof(WitsItemBase.IdItem), nameof(WitsItemBase.DateTime)); }); modelBuilder.Entity(entity => { entity.HasKey(nameof(UserSetting.IdUser), nameof(UserSetting.Key)); }); modelBuilder.Entity(entity => { entity.HasKey(x => new { x.IdWell, x.IdUser, x.IdCategory }) .HasName("t_well_final_documents_pk"); }); modelBuilder.Entity(entity => { entity.HasKey(x => new { x.IdWell, x.IdUser }); }); modelBuilder.Entity() .HasOne(mf => mf.Parent) .WithMany(mf => mf.Children) .HasForeignKey(mf => mf.IdParent) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasOne(m => m.Directory) .WithMany(f => f.Manuals) .HasForeignKey(m => m.IdDirectory) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasKey(m => new { m.Id, m.IdTelemetry }); modelBuilder.Entity(entity => { entity.Property(e => e.Params) .HasJsonConversion(); }); modelBuilder.Entity() .HasIndex(w => new { w.IdWell, w.IdSectionType }) .IsUnique(); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Author) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(p => p.Editor) .WithMany() .OnDelete(DeleteBehavior.Restrict); DefaultData.DefaultContextData.Fill(modelBuilder); } public Task RefreshMaterializedViewAsync(CancellationToken token) where TEntity : class { var materializedViewName = Set().EntityType.GetViewName() ?? throw new System.Exception($"RefreshMaterializedViewAsync<{typeof(TEntity).Name}>(..) db table for this type does not found."); return RefreshMaterializedViewAsync(materializedViewName, token); } public Task RefreshMaterializedViewAsync(string materializedViewName, CancellationToken token) { var sql = $"REFRESH MATERIALIZED VIEW {materializedViewName};"; return Database.ExecuteSqlRawAsync(sql, token); } } }