diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/GroupsController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/GroupsController.cs index 3d910144..807aa9d2 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/GroupsController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/GroupsController.cs @@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System.Collections.Generic; -using System.Linq; namespace Bootstrap.Admin.Controllers.Api { diff --git a/src/admin/Bootstrap.Admin/Extensions/AutoGenerateDatabaseExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/AutoGenerateDatabaseExtensions.cs index baf0bf8f..7a5b3ca2 100644 --- a/src/admin/Bootstrap.Admin/Extensions/AutoGenerateDatabaseExtensions.cs +++ b/src/admin/Bootstrap.Admin/Extensions/AutoGenerateDatabaseExtensions.cs @@ -2,9 +2,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System; using System.IO; using System.Linq; -using System; namespace Microsoft.AspNetCore.Builder { @@ -26,34 +26,40 @@ namespace Microsoft.AspNetCore.Builder { if (!_init) { - // 阻止所有线程继续往下运行,等待数据库检查 - lock (_locker) + // 优化性能 + var config = context.RequestServices.GetRequiredService(); + var autoGenerate = config.GetValue("AutoGenerateDatabase", false); + if (!autoGenerate) _init = true; + if (autoGenerate) { - if (!_init) + // 阻止所有线程继续往下运行,等待数据库检查 + lock (_locker) { - // 数据检查 - var config = context.RequestServices.GetRequiredService(); - var dbSection = config.GetSection("DB").GetChildren().FirstOrDefault(c => c.GetValue("Enabled", false)); - if (dbSection != null) + if (!_init) { - var folder = dbSection[SqlFolderKey]?.ReplaceOSPlatformPath(); - if (!string.IsNullOrEmpty(folder)) + // 数据检查 + var dbSection = config.GetSection("DB").GetChildren().FirstOrDefault(c => c.GetValue("Enabled", false)); + if (dbSection != null) { - // 判断文件夹是否存在 - var env = context.RequestServices.GetRequiredService(); - var fullFolder = Path.Combine(env.ContentRootPath, folder); - if (Directory.Exists(fullFolder)) + var folder = dbSection.GetValue(SqlFolderKey, "").ReplaceOSPlatformPath(); + if (!string.IsNullOrEmpty(folder)) { - try + // 判断文件夹是否存在 + var env = context.RequestServices.GetRequiredService(); + var fullFolder = Path.Combine(env.ContentRootPath, folder); + if (Directory.Exists(fullFolder)) { - AutoDbHelper.CheckDB(fullFolder); + try + { + AutoDbHelper.EnsureCreated(fullFolder); + } + catch { } } - catch { } } } } + _init = true; } - _init = true; } } await next(); diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index e2ca0071..948e68cd 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -1,10 +1,8 @@ using Bootstrap.DataAccess; -using Longbow.Web; using Longbow.Web.SignalR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; diff --git a/src/admin/Bootstrap.Admin/appsettings.Development.json b/src/admin/Bootstrap.Admin/appsettings.Development.json index 093e05c6..4c6a1cc4 100644 --- a/src/admin/Bootstrap.Admin/appsettings.Development.json +++ b/src/admin/Bootstrap.Admin/appsettings.Development.json @@ -21,6 +21,7 @@ "Url": "https://client.sdgxgz.com/api/Interface/Log" } }, + "AutoGenerateDatabase": true, "DB": [ { "Enabled": false, diff --git a/src/admin/Bootstrap.Admin/appsettings.json b/src/admin/Bootstrap.Admin/appsettings.json index 4e9dba96..7338804b 100644 --- a/src/admin/Bootstrap.Admin/appsettings.json +++ b/src/admin/Bootstrap.Admin/appsettings.json @@ -21,6 +21,7 @@ "ConnectionStrings": { "ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa" }, + "AutoGenerateDatabase": false, "DB": [ { "Enabled": false, diff --git a/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs b/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs index e633392b..690e5184 100644 --- a/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs +++ b/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs @@ -9,7 +9,7 @@ /// 数据库检查方法 /// /// - public override void CheckDB(string folder) + public override void EnsureCreated(string folder) { // UNDONE: 没有环境暂时未写代码 } diff --git a/src/admin/Bootstrap.DataAccess/AutoDB.cs b/src/admin/Bootstrap.DataAccess/AutoDB.cs index 54404f76..11f8df86 100644 --- a/src/admin/Bootstrap.DataAccess/AutoDB.cs +++ b/src/admin/Bootstrap.DataAccess/AutoDB.cs @@ -16,7 +16,7 @@ namespace Bootstrap.DataAccess /// /// 数据库检查方法 /// - public virtual void CheckDB(string folder) + public virtual void EnsureCreated(string folder) { _folder = folder; using var db = Longbow.Data.DbManager.Create(); @@ -29,7 +29,7 @@ namespace Bootstrap.DataAccess case "SqlServerDatabaseProvider": using (var newDB = ModifyConnectionString(db)) { - if (newDB.ExecuteScalar("select COUNT(1) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateSqlServer(); + if (newDB.ExecuteScalar("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateSqlServer(); } break; case "MySqlDatabaseProvider": diff --git a/src/admin/Bootstrap.DataAccess/Helper/AutoDbHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/AutoDbHelper.cs index f99416db..43281ca4 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/AutoDbHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/AutoDbHelper.cs @@ -8,6 +8,6 @@ /// /// 数据库检查方法 /// - public static void CheckDB(string folder) => DbContextManager.Create()?.CheckDB(folder); + public static void EnsureCreated(string folder) => DbContextManager.Create()?.EnsureCreated(folder); } }