Blame view

HHECS.DAQHandle/DataAccess/DataContext.cs 2.58 KB
唐召明 authored
1
2
using FreeSql;
using HHECS.DAQHandle.Models;
3
using HHECS.EquipmentModel;
唐召明 authored
4
5
6
7
8
9

namespace HHECS.DAQHandle.DataAccess
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
10
11
12
13
    /// <remarks>
    /// <para>参考文档:<a href="https://freesql.net/guide/db-context.html">https://freesql.net/guide/db-context.html</a></para>
    /// <para>注意:<see cref="DataContext"/> 对象多线程不安全</para>
    /// </remarks>
唐召明 authored
14
15
    public class DataContext : DbContext
    {
16
        public DbSet<Equipment> Equipment { get; set; } = null!;
唐召明 authored
17
18
        public DbSet<EquipmentType> EquipmentType { get; set; } = null!;
唐召明 authored
19
20
        public DbSet<EquipmentTypePropTemplate> EquipmentTypePropTemplate { get; set; } = null!;
唐召明 authored
21
22
        public DbSet<EquipmentProp> EquipmentProp { get; set; } = null!;
唐召明 authored
23
24
25
26
27

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

        public DbSet<EquipmentAlarmRecord> EquipmentAlarmRecords { get; set; } = null!;
28
        public DbSet<EquipmentStatusRecordHistory> EquipmentStatusRecordHistorys { get; set; } = null!;
唐召明 authored
29
        public DbSet<EquipmentStatusRecord> EquipmentStatusRecords { get; set; } = null!;
李璐瑶 authored
30
31
        public DbSet<Project> Project { get; set; } = null!;
        public DbSet<WareHouse> WareHouse { get; set; } = null!;
唐召明 authored
32
唐召明 authored
33
34
        public DbSet<ClientStatus> ClientStatus { get; set; } = null!;
35
36
37
38
39
        protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {
            builder.UseFreeSql(GlobalVar.FreeSql);
        }
唐召明 authored
40
        protected override void OnModelCreating(ICodeFirst codefirst)
唐召明 authored
41
        {
42
43
44
            codefirst.Entity<Equipment>(eb =>
            {
                eb.ToTable($"daq_equipment");
李璐瑶 authored
45
46

                eb.HasIndex(x => x.Code).IsUnique();
47
48
49
50
            });

            codefirst.Entity<EquipmentType>(eb =>
            {
李璐瑶 authored
51
                eb.ToTable("daq_equipment_type");
52
53
54
55
            });

            codefirst.Entity<EquipmentProp>(eb =>
            {
李璐瑶 authored
56
                eb.ToTable("daq_equipment_prop");
57
58
59
            });

            codefirst.Entity<EquipmentTypePropTemplate>(eb =>
唐召明 authored
60
            {
李璐瑶 authored
61
                eb.ToTable("daq_equipment_type_prop_template");
唐召明 authored
62
63
                eb.Property(p => p.PropType).HasColumnType("varchar(50)").Help().MapType(typeof(string));
                eb.Property(p => p.DataType).HasColumnType("varchar(50)").Help().MapType(typeof(string));
唐召明 authored
64
65
            });
唐召明 authored
66
67
68
69
            codefirst.SyncStructure<Equipment>();
            codefirst.SyncStructure<EquipmentType>();
            codefirst.SyncStructure<EquipmentProp>();
            codefirst.SyncStructure<EquipmentTypePropTemplate>();
唐召明 authored
70
71
72
        }
    }
}