using Microsoft.EntityFrameworkCore; namespace GreenHome.Infrastructure; public sealed class GreenHomeDbContext : DbContext { public GreenHomeDbContext(DbContextOptions options) : base(options) { } public DbSet Devices => Set(); public DbSet TelemetryRecords => Set(); public DbSet DeviceSettings => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity(b => { b.ToTable("Devices"); b.HasKey(x => x.Id); b.Property(x => x.DeviceName).IsRequired().HasMaxLength(10); b.Property(x => x.Owner).IsRequired(); b.Property(x => x.Mobile).IsRequired(false); b.Property(x => x.Location).HasMaxLength(250); b.Property(x => x.NeshanLocation).HasMaxLength(80); }); modelBuilder.Entity(b => { b.ToTable("Telemetry"); b.HasKey(x => x.Id); b.Property(x => x.TemperatureC).HasColumnType("decimal(18,2)"); b.Property(x => x.HumidityPercent).HasColumnType("decimal(18,2)"); b.Property(x => x.SoilPercent).HasColumnType("decimal(18,2)"); b.Property(x => x.Lux).HasColumnType("decimal(18,2)"); b.Property(x => x.PersianDate).HasMaxLength(10); b.HasIndex(x => new { x.DeviceId, x.PersianYear, x.PersianMonth }); b.HasIndex(x => new { x.DeviceId, x.TimestampUtc }); }); modelBuilder.Entity(b => { b.ToTable("DeviceSettings"); b.HasKey(x => x.Id); b.Property(x => x.DangerMaxTemperature).HasColumnType("decimal(18,2)"); b.Property(x => x.DangerMinTemperature).HasColumnType("decimal(18,2)"); b.Property(x => x.MaxTemperature).HasColumnType("decimal(18,2)"); b.Property(x => x.MinTemperature).HasColumnType("decimal(18,2)"); b.Property(x => x.MaxLux).HasColumnType("decimal(18,2)"); b.Property(x => x.MinLux).HasColumnType("decimal(18,2)"); b.Property(x => x.MaxHumidityPercent).HasColumnType("decimal(18,2)"); b.Property(x => x.MinHumidityPercent).HasColumnType("decimal(18,2)"); b.HasOne(x => x.Device) .WithMany() .HasForeignKey(x => x.DeviceId) .OnDelete(DeleteBehavior.Cascade); b.HasIndex(x => x.DeviceId).IsUnique(); }); } }