DataContext.cs 1.79 KB
using FreeSql;
using HHECS.DAQClient.Model;
using HHECS.EquipmentModel;

namespace HHECS.DAQClient.DataAccess
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    /// <remarks>参考文档:<a href="https://freesql.net/guide/db-context.html">https://freesql.net/guide/db-context.html</a></remarks>
    public sealed class DataContext : DbContext
    {
        public DbSet<CommunicationConfig> CommunicationConfigs { get; set; } = null!;

        public DbSet<Equipment> Equipment { get; set; } = null!;

        public DbSet<EquipmentProp> EquipmentProp { get; set; } = null!;

        public DbSet<EquipmentType> EquipmentType { get; set; } = null!;

        public DbSet<EquipmentTypePropTemplate> EquipmentTypePropTemplate { get; set; } = null!;

        public DbSet<EquipmentDataQueue> EquipmentDataQueue { get; set; } = null!;

        protected override void OnModelCreating(ICodeFirst codefirst)
        {
            codefirst.Entity<CommunicationConfig>(eb =>
            {
                eb.Property(p => p.CommunicationType).HasColumnType("varchar(50)");
                eb.HasData(DataSeeding.GetCommunicationConfigs());
            });

            codefirst.Entity<EquipmentType>(eb =>
            {
                eb.HasData(DataSeeding.GetEquipmentTypes());
            });

            codefirst.Entity<EquipmentTypePropTemplate>(eb =>
            {
                eb.Property(p => p.PropType).HasColumnType("varchar(50)");
                eb.Property(p => p.DataType).HasColumnType("varchar(50)");
                eb.HasData(DataSeeding.GetEquipmentTypePropTemplates());
            });

            codefirst.SyncStructure<CommunicationConfig>();
            codefirst.SyncStructure<EquipmentType>();
            codefirst.SyncStructure<EquipmentTypePropTemplate>();

        }
    }
}