namespace IStation.DAL
|
{
|
internal class ConfigHelper
|
{
|
public static string ConnectionString
|
{
|
get
|
{
|
if (string.IsNullOrEmpty(_connectionString))
|
{
|
var dbName = "Db4Schedule.db";
|
var directory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
|
var filePath = directory + "\\" + dbName;
|
if (!System.IO.File.Exists(filePath))
|
{
|
try
|
{
|
SQLiteConnection.CreateFile(filePath);
|
}
|
catch (Exception ex)
|
{
|
var errMsg = $".db 文件不存在!Path:{filePath}\r\n{ex.Message}";
|
throw new Exception(errMsg);
|
}
|
}
|
_connectionString = $"DataSource={filePath}";
|
}
|
return _connectionString;
|
}
|
}
|
|
private static string _connectionString;
|
|
static SqlSugarClient _client = null;
|
public static SqlSugarClient GetSqlSugarClient()
|
{
|
if (_client != null)
|
return _client;
|
|
|
var config = new ConnectionConfig();
|
config.ConnectionString = ConnectionString;
|
config.IsAutoCloseConnection = true;
|
config.DbType = SqlSugar.DbType.Sqlite;
|
config.ConfigureExternalServices = new ConfigureExternalServices()
|
{
|
EntityService = (property, column) =>
|
{
|
//除主键外都可空
|
if (!column.IsPrimarykey)
|
{
|
column.IsNullable = true;
|
}
|
}
|
};
|
var db = new SqlSugarClient(config);
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
{
|
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject
|
(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
Console.WriteLine();
|
};
|
|
return db;
|
}
|
|
|
|
|
}
|
}
|