DataCacheService.cs 999 Bytes
using DataAcquisition.DataAccess;
using DataAcquisition.Models;
using Microsoft.EntityFrameworkCore;

namespace DataAcquisition.Services
{
    public class DataCacheService
    {
        private readonly IDbContextFactory<DataContext> _dbContextFactory;

        public List<Equipment> Equipments { get; private set; } = new List<Equipment>();

        public DataCacheService(IDbContextFactory<DataContext> dbContextFactory)
        {
            _dbContextFactory = dbContextFactory;
            Initial();
        }

        private void Initial()
        {
            using var dbContext = _dbContextFactory.CreateDbContext();
            dbContext.Database.EnsureCreated();
            Equipments = dbContext.CommunicationConfigs.Where(x => x.Enable)
               .Include(x => x.Equipments.Where(x => x.Enable))
               .ThenInclude(x => x.EquipmentProperties.Where(x => x.Enable)).AsSplitQuery().ToList()
               .SelectMany(x => x.Equipments).ToList();
        }
    }
}