duheng
2024-07-18 7bf79c6515f27ba9be8d2b3fb4e3ceae5718e3e5
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
using SqlSugar;
 
namespace HStation.Xhs.DAL
{
    /// <summary>
    ///
    /// </summary>
    public class DbFirstHelper
    {
        public static ConnectionConfig ConnectionConfig
        {
            get { return Xhs.ConfigHelper.PostgreSqlConnectionConfig; }
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public bool Initial(out string msg)
        {
            msg = string.Empty;
            try
            {
                #region 基础模块
 
                var basicConnectConfig = ConnectionConfig;
                if (basicConnectConfig == null)
                {
                    msg = "连接配置初始化失败";
                    return false;
                }
                basicConnectConfig.ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (property, column) =>
                    {
                        //除主键外其他列都可空
                        if (!column.IsPrimarykey)
                        {
                            column.IsNullable = true;
                        }
                        if (column.DataType == StaticConfig.CodeFirst_BigString)
                        {
                            column.DataType = "character varying";
                        }
                    }
                };
 
                var basicTypeList = new List<Type>();
                //基础模块
 
                basicTypeList.Add(typeof(Yw.Entity.SysModule));
                basicTypeList.Add(typeof(Yw.Entity.SysType));
                basicTypeList.Add(typeof(Yw.Entity.SysCatalog));
                basicTypeList.Add(typeof(Yw.Entity.SysPropGroup));
                basicTypeList.Add(typeof(Yw.Entity.SysProp));
                basicTypeList.Add(typeof(Yw.Entity.SysPropMapping));
                basicTypeList.Add(typeof(Yw.Entity.SysPropChoice));
                basicTypeList.Add(typeof(Yw.Entity.SysPropValue));
                basicTypeList.Add(typeof(Yw.Entity.SysPropValuePure));
                basicTypeList.Add(typeof(Entity.PumpPartMain));
                basicTypeList.Add(typeof(Entity.PumpPropContent));
                basicTypeList.Add(typeof(Entity.PumpMainAndPartMap));
                basicTypeList.Add(typeof(Entity.PumpMain));
                basicTypeList.Add(typeof(Entity.PumpGroup));
                basicTypeList.Add(typeof(Entity.PumpGroupAndMainMap));
                basicTypeList.Add(typeof(Entity.PumpSeries));
 
                // basicTypeList.Add(typeof(Yw.Entity.SysCatalogOrg));
 
                //系统属性
 
                if (basicTypeList.Count > 0)
                {
                    using (var db = new SqlSugarClient(basicConnectConfig))
                    {
                        //设置字符串默认长度
                        //db.CodeFirst.SetStringDefaultLength(250);
                        //db.CodeFirst.SetStringDefaultLength(int.MaxValue);
                        //建库:如果不存在创建数据库存在不会重复创建 createdb;注意 :Oracle和个别国产库需不支持该方法,需要手动建库
                        db.DbMaintenance.CreateDatabase();
                        db.CodeFirst.InitTables
                            (
                                basicTypeList.ToArray()
                            );
                    }
                }
 
                #endregion 基础模块
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
    }
}