lixiaojun
2024-12-20 357693611c60d93fb17189273d4c91dab364f0d4
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace HStation.Service.Assets
{
    /// <summary>
    ///
    /// </summary>
    public class DbFirstHelper
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public static bool Initial(out string msg)
        {
            msg = string.Empty;
            try
            {
                var connectConfig = HStation.Assets.ConfigHelper.DefaultConnectionConfig;
                if (connectConfig == null)
                {
                    msg = "连接配置初始化失败";
                    return false;
                }
 
                connectConfig.ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (property, column) =>
                    {
                        //除主键外其他列都可空
                        if (!column.IsPrimarykey)
                        {
                            column.IsNullable = true;
                        }
                        if (column.DataType == StaticConfig.CodeFirst_BigString)
                        {
                            if (Settings.AssetsParasHelper.Assets.DataBase.DbType == HStation.Assets.DbType.PostgreSql)
                            {
                                column.DataType = "character varying";
                            }
                        }
                    }
                };
 
                var typeList = new List<Type>()
                {
                   typeof(Entity.AssetsPumpGroup),
                   typeof(Entity.AssetsPumpGroupAndMainMapping),
                   typeof(Entity.AssetsPumpMain),
                   typeof(Entity.AssetsPumpMainAndPartMapping),
                   typeof(Entity.AssetsPumpPartMain),
                   typeof(Entity.AssetsPumpPropContent),
                   typeof(Entity.AssetsPumpSeries),
                   typeof(Entity.AssetsPumpType),
                   typeof(Entity.AssetsPumpSeriesTypeMapping),
                   typeof(Entity.AssetsPumpSeriesManufacturerMapping),
 
                   //阀门
                   typeof(Entity.AssetsValveMain),
                   typeof(Entity.AssetsValveSeries),
                   typeof(Entity.AssetsValveFactor),
                   typeof(Entity.AssetsValvePartMain),
                   typeof(Entity.AssetsValveMainAndPartMapping),
 
                   //弯头
                   typeof(Entity.AssetsElbowMain),
                   typeof(Entity.AssetsElbowSeries),
                   typeof(Entity.AssetsElbowCoefficient),
 
                   //三通
                   typeof(Entity.AssetsThreelinkMain),
                   typeof(Entity.AssetsThreelinkSeries),
                   typeof(Entity.AssetsThreelinkCoefficient),
 
                   //四通
                   typeof(Entity.AssetsFourlinkMain),
                   typeof(Entity.AssetsFourlinkSeries),
                   typeof(Entity.AssetsFourlinkCoefficient),
 
                   //管道
                   typeof(Entity.AssetsPipeMain),
                   typeof(Entity.AssetsPipeSeries),
                   typeof(Entity.AssetsPipeCoefficient),
 
                   //喷头
                   typeof(Entity.AssetsSprinklerMain),
                   typeof(Entity.AssetsSprinklerSeries),
                   typeof(Entity.AssetsSprinklerCoefficient),
 
                   //厂商
                   typeof(Entity.AssetsManufacturer),
                   typeof(Entity.AssetsManufacturerSeries),
 
                   //自定义
                    typeof(Entity.AssetsUserDefined),
 
                   //成套设备
                   typeof(Entity.AssetsPackageMain),
                   typeof(Entity.AssetsPackagePumpMapping),
                   typeof(Entity.AssetsPackageSeries),
                   typeof(Entity.AssetsPackageMfrMapping),
                   typeof(Entity.AssetsPackagePartMain),
                   typeof(Entity.AssetsPackageMainAndPartMapping),
                   typeof(Entity.AssetsPackagePropContent),
 
                   //闷头
                   typeof(Entity.AssetsBluntheadMain),
                   typeof(Entity.AssetsBluntheadSeries),
                   typeof(Entity.AssetsBluntheadCoefficient),
 
                   //水池
                   typeof(Entity.AssetsTankMain),
                   typeof(Entity.AssetsTankSeries),
                   typeof(Entity.AssetsTankCoefficient),
                   typeof(Entity.AssetsTankPartMain),
                   typeof(Entity.AssetsTankMainAndPartMapping),
 
                   //消火栓
                   typeof(Entity.AssetsHydrantMain),
                   typeof(Entity.AssetsHydrantSeries),
                   typeof(Entity.AssetsHydrantCoefficient),
 
                   //过渡件
                   typeof(Entity.AssetsTranslationMain),
                   typeof(Entity.AssetsTranslationSeries),
                   typeof(Entity.AssetsTranslationCoefficient),
 
                   //水表
                   typeof(Entity.AssetsMeterSeries),
                   typeof(Entity.AssetsMeterMain),
                   typeof(Entity.AssetsMeterFactor),
 
                   //流量计
                   typeof(Entity.AssetsFlowmeterSeries),
                   typeof(Entity.AssetsFlowmeterMain),
                   typeof(Entity.AssetsFlowmeterFactor),
 
                   //压力表
                   typeof(Entity.AssetsPressmeterSeries),
                   typeof(Entity.AssetsPressmeterMain),
                   typeof(Entity.AssetsPressmeterFactor),
 
                   //换热器
                   typeof(Entity.AssetsExchangerSeries),
                   typeof(Entity.AssetsExchangerMain),
                   typeof(Entity.AssetsExchangerFactor),
 
                   //压缩机
                   typeof(Entity.AssetsCompressorSeries),
                   typeof(Entity.AssetsCompressorMain),
                   typeof(Entity.AssetsCompressorFactor),
 
                   //冷却塔
                   typeof(Entity.AssetsCoolingSeries),
                   typeof(Entity.AssetsCoolingMain),
                   typeof(Entity.AssetsCoolingFactor),
                 };
 
                using (var db = new SqlSugarClient(connectConfig))
                {
                    //设置字符串默认长度
                    //db.CodeFirst.SetStringDefaultLength(250);
                    //db.CodeFirst.SetStringDefaultLength(int.MaxValue);
                    //建库:如果不存在创建数据库存在不会重复创建 createdb;注意 :Oracle和个别国产库需不支持该方法,需要手动建库
                    db.DbMaintenance.CreateDatabase();
                    db.CodeFirst.InitTables
                        (
                           typeList.ToArray()
                        );
                }
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
    }
}