DataContext.cs 2.24 KB
using DataAcquisition.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace DataAcquisition.DataAccess
{
    public sealed class DataContext(DbContextOptions options) : DbContext(options)
    {
        public DbSet<WorkpieceModel> WorkpieceModels => Set<WorkpieceModel>();

        public DbSet<Equipment> Equipments => Set<Equipment>();

        public DbSet<EquipmentProperty> EquipmentProperties => Set<EquipmentProperty>();

        public DbSet<CommunicationConfig> CommunicationConfigs => Set<CommunicationConfig>();

        public DbSet<EquipmentPropertyRecord> EquipmentPropertyRecords => Set<EquipmentPropertyRecord>();

        public DbSet<WorkpieceProduction> WorkpieceProductions => Set<WorkpieceProduction>();

        public DbSet<EquipmentDataQueue> EquipmentDataQueues => Set<EquipmentDataQueue>();

        public DbSet<WeldProcessRecord> WeldProcessRecords => Set<WeldProcessRecord>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<EquipmentPropertyRecord>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());
            modelBuilder.Entity<WorkpieceProduction>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());
            modelBuilder.Entity<EquipmentDataQueue>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());
            modelBuilder.Entity<WeldProcessRecord>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());

            modelBuilder.Entity<CommunicationConfig>().Property(x => x.CommunicationType).HasConversion<string>();
            modelBuilder.Entity<Equipment>().Property(x => x.EquipmentType).HasConversion<string>();
            modelBuilder.Entity<EquipmentProperty>().Property(x => x.DataType).HasConversion<string>();

            modelBuilder.Entity<CommunicationConfig>().HasData(DataSeeding.GetCommunicationConfigs());
            modelBuilder.Entity<Equipment>().HasData(DataSeeding.GetEquipments());
            modelBuilder.Entity<EquipmentProperty>().HasData(DataSeeding.GetEquipmentProperties());
            base.OnModelCreating(modelBuilder);
        }
    }
}