|
|
|
|
using Custom.Utils.Framework;
|
|
|
|
|
using ProductionSystem_Log;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace ProductionSystem_Service
|
|
|
|
|
{
|
|
|
|
|
public partial class DbContext
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接符字串
|
|
|
|
|
/// $"Server={Server};Port={Port};Database={Database};User Id={UserId};Password={passWord}"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string ConnStr = "";
|
|
|
|
|
|
|
|
|
|
public static SqlSugarScope db = null;
|
|
|
|
|
|
|
|
|
|
public static void GetConStr(string server, string port, string database, string userId, string passWord, bool isEncrypt, string schema)
|
|
|
|
|
{
|
|
|
|
|
if (isEncrypt)
|
|
|
|
|
{
|
|
|
|
|
// 解密
|
|
|
|
|
passWord = DesHelper.DecryptStringFromBytes_Aes(passWord);
|
|
|
|
|
}
|
|
|
|
|
ConnStr = $"Server={server};Port={port};Database={database};User Id={userId};Password={passWord};Search Path={schema}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbContext()
|
|
|
|
|
{
|
|
|
|
|
if (db == null)
|
|
|
|
|
{
|
|
|
|
|
//用单例模式
|
|
|
|
|
db = new SqlSugarScope(new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = ConnStr,//连接符字串
|
|
|
|
|
DbType = DbType.PostgreSQL, //数据库类型
|
|
|
|
|
IsAutoCloseConnection = true, //不设成true要手动close
|
|
|
|
|
},
|
|
|
|
|
db =>
|
|
|
|
|
{
|
|
|
|
|
//(A)全局生效配置点
|
|
|
|
|
//调试SQL事件,可以删掉
|
|
|
|
|
//db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
//{
|
|
|
|
|
// Console.WriteLine(sql);//输出sql,查看执行sql
|
|
|
|
|
//};
|
|
|
|
|
db.Aop.OnError = (exp) =>
|
|
|
|
|
{
|
|
|
|
|
LogHelper.Error(exp, exp.Sql);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
|
|
|
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|