tangxu
2024-04-30 26d8996e55c33186800031f7c305688035bb3182
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using SqlSugar;
using System;
using System.Data.SQLite;
using System.Linq;
 
 
namespace AStation.DAL
{
    public class DbInitialT
    {
        /// <summary>
        /// 初始数据库
        /// </summary> 
        /// <param name="backup"></param>
        /// <param name="stringDefaultLength"></param>
        /// <returns></returns>
        public static bool InitTables(bool backup = false, int stringDefaultLength = 250)
        {
            var db = GetDb();
            if (db == null)
                return false;
 
            Type[] types = new Type[] {
                       typeof(Entity.SignalType),
                       typeof(Entity.SignalTypeGroup),
                       typeof(Entity.ElecPrice),
 
                       typeof(Entity.Station),
 
                       typeof(Entity.Equipment),
                       typeof(Entity.EquipmentGroup),
                       typeof(Entity.EquipmentMonitorMapping),
                       typeof(Entity.EquipmentChartMapping),
 
                       typeof(Entity.LoginUser),
 
 
                       typeof(Entity.MonitorPoint),
                       typeof(Entity.MonitorPointGroup),
                       typeof(Entity.Signal),
 
                        typeof(Entity.MonitorDataSources),
                        typeof(Entity.SoftSetting),
 
                   };
 
            //Db.CodeFirst.SetStringDefaultLength(StringDefaultLength); 
            db.DbMaintenance.CreateDatabase();
            if (backup)
            {
                db.CodeFirst.BackupTable().InitTables(types);
            }
            else
            {
                db.CodeFirst.InitTables(types);
            }
            return true;
        }
 
        private static SqlSugarClient GetDb()
        {
            var dbName = "Db4Basic.db";
            var filePath = $"{FileHelper.GetWorkFolder()}\\{dbName}";
            if (!System.IO.File.Exists(filePath))
            {
                try
                {
                    SQLiteConnection.CreateFile(filePath);
                }
                catch (Exception ex)
                {
                    var errMsg = $"{dbName}文件不存在!Path:{filePath}\r\n{ex.Message}";
                    LogHelper.Debug(errMsg);
                    throw new Exception(errMsg);
                }
            }
 
            var config = new ConnectionConfig();
            config.ConnectionString = $"DataSource={filePath}";
            config.IsAutoCloseConnection = true;
            config.DbType = SqlSugar.DbType.Sqlite;
            config.ConfigureExternalServices = new ConfigureExternalServices()
            {
                EntityService = (property, column) =>
                {
                    //除主键外都可空
                    if (!column.IsPrimarykey)
                    {
                        column.IsNullable = true;
                    }
                }
            };
 
            var db = new SqlSugarClient(config);
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject
                    (pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
 
            return db;
        }
 
    }
 
}