diff --git a/PrivateBox/DataContext/DbContext.cs b/PrivateBox/DataContext/DbContext.cs deleted file mode 100644 index cf32b01..0000000 --- a/PrivateBox/DataContext/DbContext.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Falcon.SugarCore.DbContextExtensions; -using PrivateBox.DataContext; -using SqlSugar; -using System.Text; - -namespace PrivateBox -{ - /// - /// 数据库上下文 - /// - public class DbContext:Falcon.SugarCore.DbContextExtensions.DbContext - { - public AppConfig Config { get; init; } - - /// - /// 数据库是否已经初始化 - /// - private static bool IsInited = false; - - public DbContext(DbContextConfig config):base(config) { - DbInit(); - } - - //public DbContext(AppConfig config) : base(new ConnectionConfig { - // DbType = DbType.Sqlite, - // ConnectionString = config.SqliteFileName, - // IsAutoCloseConnection = false, - - //}) { - // this.Config = config; - // DbInit(); - //} - - /// - /// 初始化数据库 - /// - public void DbInit() { - if(IsInited) { - return; - } - this.CodeFirst.SetStringDefaultLength(200); - this.CodeFirst.InitTables(); - this.CodeFirst.InitTables(); - this.CodeFirst.InitTables(); - IsInited = true; - } - - /// - /// 获取加密用的key - /// - /// 加密用的key - public string GetCrytionKey() { - var qu = this.Queryable().ToList(); - if(qu.Any() && !string.IsNullOrEmpty(qu.First().Key)) { - return qu.First().Key; - } - var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnobqrstuvwxyz1234567890".ToArray(); - StringBuilder key = new(36); - Random rd = new Random(); - for(int i = 0;i < key.Capacity;i++) { - int index = (int)rd.NextInt64(0,keyChars.Length); - var k = keyChars[index]; - key.Append(k); - } - this.Insertable(new CryptionKey { Key = key.ToString() }).ExecuteCommand(); - return key.ToString(); - } - } -} diff --git a/PrivateBox/DataContext/DbContextExtension.cs b/PrivateBox/DataContext/DbContextExtension.cs new file mode 100644 index 0000000..cd5f8d3 --- /dev/null +++ b/PrivateBox/DataContext/DbContextExtension.cs @@ -0,0 +1,31 @@ +using Falcon.SugarCore.DbContextExtensions; +using PrivateBox.DataContext; +using SqlSugar; +using System.Text; + +namespace PrivateBox +{ + public static class DbContextExtension + { + /// + /// 获取加密用的key + /// + /// 加密用的key + public static string GetCrytionKey(this DbContext db) { + var qu = db.Queryable().ToList(); + if(qu.Any() && !string.IsNullOrEmpty(qu.First().Key)) { + return qu.First().Key; + } + var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnobqrstuvwxyz1234567890".ToArray(); + StringBuilder key = new(36); + Random rd = new Random(); + for(int i = 0;i < key.Capacity;i++) { + int index = (int)rd.NextInt64(0,keyChars.Length); + var k = keyChars[index]; + key.Append(k); + } + db.Insertable(new CryptionKey { Key = key.ToString() }).ExecuteCommand(); + return key.ToString(); + } + } +} diff --git a/PrivateBox/DataContext/DbInitializer.cs b/PrivateBox/DataContext/DbInitializer.cs new file mode 100644 index 0000000..7192446 --- /dev/null +++ b/PrivateBox/DataContext/DbInitializer.cs @@ -0,0 +1,26 @@ +using Falcon.SugarCore.DbContextExtensions; + +namespace PrivateBox.DataContext +{ + /// + /// 数据库初始化 + /// + public static class DbInitializer + { + private static int Inited = 0; + + /// + /// 执行初始化,全局仅运行一次 + /// + public static void Init(DbContext db) { + + if(Interlocked.CompareExchange(ref Inited,1,0) == 0) { + db.CodeFirst.SetStringDefaultLength(200); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + + } + } + } +}