DataContext.cs
2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using FreeSql;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;
namespace HHECS.DAQHandle.DataAccess
{
/// <summary>
/// 数据库上下文
/// </summary>
/// <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>
public class DataContext : DbContext
{
public DbSet<Equipment> Equipment { get; set; } = null!;
public DbSet<EquipmentType> EquipmentType { get; set; } = null!;
public DbSet<EquipmentTypePropTemplate> EquipmentTypePropTemplate { get; set; } = null!;
public DbSet<EquipmentProp> EquipmentProp { get; set; } = null!;
public DbSet<EquipmentDataRecord> EquipmentDataRecord { get; set; } = null!;
public DbSet<EquipmentAlarmRecord> EquipmentAlarmRecords { get; set; } = null!;
public DbSet<EquipmentStatusRecordHistory> EquipmentStatusRecordHistorys { get; set; } = null!;
public DbSet<EquipmentStatusRecord> EquipmentStatusRecords { get; set; } = null!;
public DbSet<Project> Project { get; set; } = null!;
public DbSet<WareHouse> WareHouse { get; set; } = null!;
public DbSet<ClientStatus> ClientStatus { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseFreeSql(GlobalVar.FreeSql);
}
protected override void OnModelCreating(ICodeFirst codefirst)
{
codefirst.Entity<Equipment>(eb =>
{
eb.ToTable($"daq_equipment");
eb.HasIndex(x => x.Code).IsUnique();
});
codefirst.Entity<EquipmentType>(eb =>
{
eb.ToTable("daq_equipment_type");
});
codefirst.Entity<EquipmentProp>(eb =>
{
eb.ToTable("daq_equipment_prop");
});
codefirst.Entity<EquipmentTypePropTemplate>(eb =>
{
eb.ToTable("daq_equipment_type_prop_template");
eb.Property(p => p.PropType).HasColumnType("varchar(50)").Help().MapType(typeof(string));
eb.Property(p => p.DataType).HasColumnType("varchar(50)").Help().MapType(typeof(string));
});
codefirst.SyncStructure<Equipment>();
codefirst.SyncStructure<EquipmentType>();
codefirst.SyncStructure<EquipmentProp>();
codefirst.SyncStructure<EquipmentTypePropTemplate>();
}
}
}