chore: 优化 sqlsugar 配置

main
NoahLan 6 months ago
parent 1d0922b473
commit 45a9862edf

@ -24,6 +24,28 @@ public class NPinFrameworkSqlSugarCoreModule : AbpModule
var configuration = service.GetConfiguration(); var configuration = service.GetConfiguration();
Configure<DbConnOptions>(configuration.GetSection("DbConnOptions")); Configure<DbConnOptions>(configuration.GetSection("DbConnOptions"));
if (Enum.TryParse<DbType>(
configuration.GetSection(nameof(DbConnOptions)).GetSection(nameof(DbConnOptions.DbType)).Value,
out var dbType))
{
Configure<AbpSequentialGuidGeneratorOptions>(opt =>
{
switch (dbType)
{
case DbType.PostgreSQL:
case DbType.MySql:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString;
break;
case DbType.Oracle:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAsBinary;
break;
default:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAtEnd;
break;
}
});
}
// 开放 sqlSugarClient // 开放 sqlSugarClient
service.TryAddScoped<ISqlSugarDbContext, SqlSugarDbContext>(); service.TryAddScoped<ISqlSugarDbContext, SqlSugarDbContext>();
// 不开放 sqlSugarClient // 不开放 sqlSugarClient
@ -45,23 +67,6 @@ public class NPinFrameworkSqlSugarCoreModule : AbpModule
var service = context.ServiceProvider; var service = context.ServiceProvider;
var options = service.GetRequiredService<IOptions<DbConnOptions>>().Value; var options = service.GetRequiredService<IOptions<DbConnOptions>>().Value;
Configure<AbpSequentialGuidGeneratorOptions>(opt =>
{
switch (options.DbType)
{
case DbType.PostgreSQL:
case DbType.MySql:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString;
break;
case DbType.Oracle:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAsBinary;
break;
default:
opt.DefaultSequentialGuidType = SequentialGuidType.SequentialAtEnd;
break;
}
});
var logger = service.GetRequiredService<ILogger<NPinFrameworkSqlSugarCoreModule>>(); var logger = service.GetRequiredService<ILogger<NPinFrameworkSqlSugarCoreModule>>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

@ -90,11 +90,6 @@ public class SqlSugarDbConnectionCreator : ISqlSugarDbConnectionCreator, ITransi
{ {
EntityService = (c, p) => EntityService = (c, p) =>
{ {
if (new NullabilityInfoContext().Create(c).WriteState is NullabilityState.Nullable)
{
p.IsNullable = true;
}
EntityService(c, p); EntityService(c, p);
}, },
EntityNameService = (t, e) => EntityNameService = (t, e) =>

@ -219,28 +219,27 @@ public class SqlSugarDbContext : ISqlSugarDbContext
protected virtual void OnLogExecuting(string sql, SugarParameter[] pars) protected virtual void OnLogExecuting(string sql, SugarParameter[] pars)
{ {
if (Options.EnabledSqlLog)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("==========NPin-SQL==========");
sb.AppendLine(UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
sb.AppendLine("============================");
Logger.CreateLogger<SqlSugarDbContext>().LogDebug(sb.ToString());
}
} }
protected virtual void OnLogExecuted(string sql, SugarParameter[] pars) protected virtual void OnLogExecuted(string sql, SugarParameter[] pars)
{ {
if (Options.EnabledSqlLog) if (Options.EnabledSqlLog)
{ {
var sqllog = $"=========NPin-SQL耗时{SqlSugarClient.Ado.SqlExecutionTime.TotalMilliseconds}毫秒====="; var log = Logger.CreateLogger<SqlSugarDbContext>();
Logger.CreateLogger<SqlSugarDbContext>().LogDebug(sqllog);
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("==========NPin-SQL==========");
sb.AppendLine(UtilMethods.GetNativeSql(sql, pars));
sb.AppendLine($"Count: {SqlSugarClient.Ado.SqlExecuteCount}");
sb.AppendLine($"耗时 {SqlSugarClient.Ado.SqlExecutionTime.TotalMilliseconds} 毫秒");
log.LogDebug(sb.ToString());
} }
} }
/// <summary> /// <summary>
/// 实体配置 /// 实体配置
/// 自动 Nullable
/// 自动主键 /// 自动主键
/// 自动Ignore关联配置导航 /// 自动Ignore关联配置导航
/// 开启下划线 /// 开启下划线
@ -249,6 +248,12 @@ public class SqlSugarDbContext : ISqlSugarDbContext
/// <param name="column"></param> /// <param name="column"></param>
protected virtual void EntityService(PropertyInfo property, EntityColumnInfo column) protected virtual void EntityService(PropertyInfo property, EntityColumnInfo column)
{ {
// 自动 Nullable
if (new NullabilityInfoContext().Create(property).WriteState is NullabilityState.Nullable)
{
column.IsNullable = true;
}
if (property.PropertyType == typeof(ExtraPropertyDictionary)) if (property.PropertyType == typeof(ExtraPropertyDictionary))
{ {
column.IsIgnore = true; column.IsIgnore = true;

Loading…
Cancel
Save