Files
GreenHomeBack/src/GreenHome.Infrastructure/GreenHomeDbContext.cs
2025-10-31 20:21:22 +03:30

61 lines
2.7 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace GreenHome.Infrastructure;
public sealed class GreenHomeDbContext : DbContext
{
public GreenHomeDbContext(DbContextOptions<GreenHomeDbContext> options) : base(options) { }
public DbSet<Domain.Device> Devices => Set<Domain.Device>();
public DbSet<Domain.TelemetryRecord> TelemetryRecords => Set<Domain.TelemetryRecord>();
public DbSet<Domain.DeviceSettings> DeviceSettings => Set<Domain.DeviceSettings>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Domain.Device>(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<Domain.TelemetryRecord>(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<Domain.DeviceSettings>(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();
});
}
}