Skip to content

Commit

Permalink
🌱 6.0 正式版本
Browse files Browse the repository at this point in the history
  • Loading branch information
zgcwkj committed Nov 12, 2022
1 parent e4b6308 commit f979a7b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion zgcwkj.Model/zgcwkj.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
71 changes: 43 additions & 28 deletions zgcwkj.Util/Data/DataBase/DbCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DbCommon : DbContext, IDisposable
private DbType dbType { get; }

/// <summary>
/// 连接字符
/// 连接字符串
/// </summary>
private string dbConnect { get; }

Expand All @@ -31,9 +31,9 @@ public class DbCommon : DbContext, IDisposable
private int dbTimeout { get; }

/// <summary>
/// 数据库版本
/// 数据库自定义参数
/// </summary>
private int dbVersion { get; }
private Dictionary<string, string> dbParameters { get; set; } = new();

/// <summary>
/// <para>数据库连接对象</para>
Expand All @@ -42,14 +42,8 @@ public class DbCommon : DbContext, IDisposable
public DbCommon()
{
this.dbType = DbFactory.Type;
this.dbConnect = DbFactory.Connect;
this.dbConnect = InitParameters(DbFactory.Connect);
this.dbTimeout = DbFactory.Timeout;
this.dbVersion = 0;
if (Regex.Match(DbFactory.Connect, "version=.+?;").Value.IsNotNull())
{
this.dbConnect = DbFactory.Connect.Replace(Regex.Match(DbFactory.Connect, "version=.+?;").Value, "");
this.dbVersion = Regex.Match(DbFactory.Connect, "(?<=version=).+?(?=;)").Value.ToInt();
}
}

/// <summary>
Expand All @@ -63,17 +57,11 @@ public DbCommon()
public DbCommon(DbType dbType, string dbConnect, int dbTimeout = 10)
{
this.dbType = dbType;
this.dbConnect = dbConnect;
this.dbConnect = InitParameters(dbConnect);
this.dbTimeout = dbTimeout == 10 ? dbTimeout : DbFactory.Timeout;
this.dbVersion = 0;
if (Regex.Match(dbConnect, "version=.+?;").Value.IsNotNull())
{
this.dbConnect = dbConnect.Replace(Regex.Match(dbConnect, "version=.+?;").Value, "");
this.dbVersion = Regex.Match(dbConnect, "(?<=version=).+?(?=;)").Value.ToInt();
}
}

/// <summary>-
/// <summary>
/// <para>数据库连接对象</para>
/// <para><b>使用自定义配置来连接,数据库类型相同</b></para>
/// <para>用法:base("SQLConnect")</para>
Expand All @@ -83,14 +71,37 @@ public DbCommon(DbType dbType, string dbConnect, int dbTimeout = 10)
public DbCommon(string dbConnect, int dbTimeout = 10)
{
this.dbType = DbFactory.Type;
this.dbConnect = dbConnect;
this.dbConnect = InitParameters(dbConnect);
this.dbTimeout = dbTimeout == 10 ? dbTimeout : DbFactory.Timeout;
this.dbVersion = 0;
if (Regex.Match(dbConnect, "version=.+?;").Value.IsNotNull())
}

/// <summary>
/// 初始化参数
/// </summary>
/// <param name="connect">连接字符串(含自定义参数)</param>
/// <returns>连接字符串</returns>
private string InitParameters(string connect)
{
//自定义参数
var paramKeys = new List<string>()
{
this.dbConnect = dbConnect.Replace(Regex.Match(dbConnect, "version=.+?;").Value, "");
this.dbVersion = Regex.Match(dbConnect, "(?<=version=).+?(?=;)").Value.ToInt();
"version",
"olddatetimebehavior",
};
//取出自定义参数
foreach (var paramKey in paramKeys)
{
var dataStr = Regex.Match(connect, $"{paramKey}=.+?;").Value;
var dataValue = Regex.Match(connect, $"(?<={paramKey}=).+?(?=;)").Value;
if (dataStr.IsNotNull())
{
//清除连接字符串
connect = connect.Replace(dataStr, "");
//存储自定义参数
dbParameters.Add(paramKey, dataValue);
}
}
return connect;
}

/// <summary>
Expand All @@ -110,17 +121,21 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//PostgreSql
else if (dbType == DbType.PostgreSql)
{
//时间兼容
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
//
optionsBuilder.UseNpgsql(dbConnect, p =>
{
p.CommandTimeout(dbTimeout);
//指定数据库版本
if (this.dbVersion > 0)
if (dbParameters.ContainsKey("version"))
{
p.SetPostgresVersion(dbParameters["version"].ToInt(), 0);
}
//时间兼容(旧的时间行为)
if (dbParameters.ContainsKey("olddatetimebehavior"))
{
p.SetPostgresVersion(this.dbVersion, 0);
var oldDateTimeBehavior = dbParameters["olddatetimebehavior"].ToBool();
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", oldDateTimeBehavior);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", oldDateTimeBehavior);
}
});
}
Expand Down
14 changes: 7 additions & 7 deletions zgcwkj.Util/zgcwkj.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.6.70" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" />
</ItemGroup>

<ItemGroup>
Expand All @@ -55,10 +55,10 @@
</ItemGroup>

<ItemGroup>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>

0 comments on commit f979a7b

Please sign in to comment.