diff --git a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 885e950..02c210a 100644 --- a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -317,6 +317,7 @@ public static class ServiceCollectionExtensions this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment hostEnvironment, + List? connectionConfigs = null, Action? onDataChanging = null, Action? onDiffLogEvent = null ) @@ -356,21 +357,35 @@ public static class ServiceCollectionExtensions ArgumentException.ThrowIfNullOrEmpty(password); var connectionString = $"server={server};port={port};database={database};userid={user};password={password};"; - - var connectionConfig = new ConnectionConfig() + connectionConfigs ??= []; + connectionConfigs.Add(new ConnectionConfig() { DbType = DbType.PostgreSQL, ConnectionString = connectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, - MoreSettings = new ConnMoreSettings() + MoreSettings = new ConnMoreSettings { PgSqlIsAutoToLower = false, PgSqlIsAutoToLowerCodeFirst = false, + }, + ConfigureExternalServices = new ConfigureExternalServices() + { + EntityService = (p, c) => + { + if (c.IsPrimarykey == false && new NullabilityInfoContext() + .Create(p).WriteState is NullabilityState.Nullable) + { + c.IsNullable = true; + } + + c.DbColumnName = UtilMethods.ToUnderLine(c.DbColumnName); + }, + EntityNameService = (p, c) => { c.DbTableName = UtilMethods.ToUnderLine(c.DbTableName); } } - }; + }); - var sugarScope = new SqlSugarScope(connectionConfig, client => + var sugarScope = new SqlSugarScope(connectionConfigs, client => { client.QueryFilter.AddTableFilter(d => !d.IsDeleted); if (hostEnvironment.IsDevelopment() || hostEnvironment.IsStaging())