增强应用程序异常处理机制,新增未处理异常日志记录功能,确保在启动和运行期间捕获并记录异常信息。同时,重构配置文件加载逻辑,支持用户目录覆盖默认配置,优化 SQLite 数据库连接字符串处理,确保在不同环境下的兼容性和稳定性。
This commit is contained in:
@@ -10,6 +10,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using System.Reflection;
|
||||
@@ -19,7 +20,9 @@ using Yitter.IdGenerator;
|
||||
using YY.Admin.Core;
|
||||
using YY.Admin.Core.Extension;
|
||||
using YY.Admin.Core.Option;
|
||||
using YY.Admin.Core.SeedData;
|
||||
using YY.Admin.Core.Session;
|
||||
using YY.Admin.Core.Util;
|
||||
using DbType = SqlSugar.DbType;
|
||||
|
||||
namespace YY.Admin.Core.SqlSugar
|
||||
@@ -160,6 +163,12 @@ namespace YY.Admin.Core.SqlSugar
|
||||
config.ConnectionString = CryptogramUtil.Decrypt(config.ConnectionString);
|
||||
}
|
||||
|
||||
// SQLite 相对路径默认依赖进程工作目录;快捷方式/从不同目录启动会在别处生成空库,表现为发布后菜单全空
|
||||
if (config.DbType == DbType.Sqlite && !string.IsNullOrWhiteSpace(config.ConnectionString))
|
||||
{
|
||||
config.ConnectionString = ResolveSqliteConnectionToAbsolutePath(config.ConnectionString);
|
||||
}
|
||||
|
||||
var configureExternalServices = new ConfigureExternalServices
|
||||
{
|
||||
EntityNameService = (type, entity) => // 处理表
|
||||
@@ -250,6 +259,96 @@ namespace YY.Admin.Core.SqlSugar
|
||||
//if (config.DbSettings.EnableInitView) InitView(dbProvider);
|
||||
// 初始化种子数据
|
||||
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
|
||||
// 关闭全量种子时首启可能无菜单数据;补一份基准菜单,避免打包版本左侧空白
|
||||
EnsureBaselineSysMenuSeed(db, config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 SQLite 连接串中的相对 DataSource 解析为基于应用程序基目录的绝对路径。
|
||||
/// </summary>
|
||||
private static string ResolveSqliteConnectionToAbsolutePath(string connectionString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var builder = new SqliteConnectionStringBuilder(connectionString);
|
||||
var ds = builder.DataSource;
|
||||
if (string.IsNullOrWhiteSpace(ds))
|
||||
{
|
||||
return connectionString;
|
||||
}
|
||||
|
||||
if (Path.IsPathFullyQualified(ds))
|
||||
{
|
||||
return connectionString;
|
||||
}
|
||||
|
||||
// Program Files 等安装目录对普通用户只读;SQLite 库放到 LocalApplicationData
|
||||
var dataDir = AppWritablePaths.EnsureDirectoryExists(AppWritablePaths.DataDirectory);
|
||||
var fileName = Path.GetFileName(ds.TrimStart('.', '/', '\\'));
|
||||
if (string.IsNullOrWhiteSpace(fileName))
|
||||
{
|
||||
fileName = "Admin.NET.db";
|
||||
}
|
||||
|
||||
builder.DataSource = Path.Combine(dataDir, fileName);
|
||||
return builder.ConnectionString;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return connectionString;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 未启用 EnableInitSeed 且 sys_menu 为空时,写入 SysMenu 种子,避免发布后界面无功能菜单。
|
||||
/// </summary>
|
||||
private static void EnsureBaselineSysMenuSeed(SqlSugarScope db, DbConnectionConfig config)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (config.SeedSettings.EnableInitSeed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.Equals(config.ConfigId.ToString(), SqlSugarConst.MainConfigId, StringComparison.Ordinal))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.DbType != DbType.Sqlite)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dbProvider = db.GetConnectionScope(config.ConfigId);
|
||||
var menuEntityInfo = dbProvider.EntityMaintenance.GetEntityInfo(typeof(SysMenu));
|
||||
if (!dbProvider.DbMaintenance.IsAnyTable(menuEntityInfo.DbTableName, false))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var cnt = dbProvider.Queryable<SysMenu>().ClearFilter().Count();
|
||||
if (cnt > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var seedType = typeof(SysMenuSeedData);
|
||||
var seedData = GetSeedData(seedType)?.ToList();
|
||||
if (seedData == null || seedData.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AdjustSeedDataIds(seedData, config);
|
||||
var progress = 0;
|
||||
InsertOrUpdateSeedData(dbProvider, seedType, typeof(SysMenu), seedData, config, ref progress, 1);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 启动阶段不因兜底种子失败而阻断
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user