DataContext.cs 1.82 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<WorkpieceProcessingRecord> WorkpieceProcessingRecords => Set<WorkpieceProcessingRecord>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<EquipmentPropertyRecord>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());
            modelBuilder.Entity<WorkpieceProcessingRecord>(entity => entity.Property(e => e.Id).HasValueGenerator<SequentialGuidValueGenerator>());
            modelBuilder.Entity<CommunicationConfig>().Property(x => x.CommunicationType).HasConversion<string>();
            modelBuilder.Entity<CommunicationConfig>().Property(x => x.EquipmentModel).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);
        }
    }
}