persistence/Persistence.Database.Postgres/PersistenceDbContext.cs

59 lines
1.7 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
2024-11-22 17:52:15 +05:00
using Persistence.Database.Entity;
namespace Persistence.Database.Model;
public partial class PersistenceDbContext : DbContext
{
public DbSet<DataSaub> DataSaub => Set<DataSaub>();
2024-11-25 10:05:23 +05:00
public DbSet<ChangeLog> ChangeLog => Set<ChangeLog>();
2024-12-05 11:30:07 +05:00
public DbSet<Setpoint> Setpoint => Set<Setpoint>();
2024-12-05 11:30:07 +05:00
public DbSet<TechMessage> TechMessage => Set<TechMessage>();
2024-12-05 11:30:07 +05:00
public DbSet<TimestampedSet> TimestampedSets => Set<TimestampedSet>();
2024-11-22 17:52:15 +05:00
public PersistenceDbContext()
: base()
{
2024-12-05 11:30:07 +05:00
}
public PersistenceDbContext(DbContextOptions<PersistenceDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseNpgsql("Host=localhost;Database=persistence;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");
2024-11-22 17:52:15 +05:00
modelBuilder.Entity<TimestampedSet>()
.Property(e => e.Set)
.HasJsonConversion();
2024-12-02 18:14:45 +05:00
modelBuilder.Entity<TechMessage>(entity =>
{
entity.HasOne(t => t.System)
.WithMany()
.HasForeignKey(t => t.SystemId)
.OnDelete(DeleteBehavior.Cascade)
2024-12-05 11:30:07 +05:00
.IsRequired();
});
2024-12-05 11:15:18 +05:00
2024-12-02 18:14:45 +05:00
modelBuilder.Entity<ChangeLog>()
2024-12-05 11:30:07 +05:00
.Property(e => e.Value)
.HasJsonConversion();
2024-11-22 17:52:15 +05:00
}
}