DeviceManager/DeviceRepair.DataAccess/DbContext.cs
2024-05-30 23:52:57 +08:00

191 lines
5.6 KiB
C#

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
namespace DeviceRepair.DataAccess
{
public class DbContext<T> where T : class, new()
{
private static readonly string connectionString = Utils.Config.Configurations.Properties.Conn;
private static readonly string logConn = Utils.Config.Configurations.Properties.logConn;
private static readonly string sfc = Utils.Config.Configurations.Properties.SfcConn;
public SqlSugarClient db;
//{
// get
// {
// var _db = new SqlSugarClient(new List<ConnectionConfig> {
// new ConnectionConfig()
// {
// ConnectionString = connectionString,
// DbType = DbType.SqlServer,
// InitKeyType = InitKeyType.Attribute,
// IsAutoCloseConnection = true,
// ConfigId = "main"
// },
// new ConnectionConfig()
// {
// ConnectionString = logConn,
// DbType = DbType.SqlServer,
// InitKeyType = InitKeyType.Attribute,
// IsAutoCloseConnection = true,
// ConfigId = "log"
// }
// });
// _db.Aop.OnLogExecuting = (sql, pars) =>
// {
// Debug.WriteLine(sql + "\r\n" +
// db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
// };
// return _db;
// }
//}
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(db); } }
public DbContext()
{
SqlConnectionStringBuilder TsSFCdb = new SqlConnectionStringBuilder(sfc);
TsSFCdb.InitialCatalog = "SFCData";
string SFCData = TsSFCdb.ConnectionString;
TsSFCdb.InitialCatalog = "SFCAddOn";
string SFCAddOn = TsSFCdb.ConnectionString;
db = new SqlSugarClient(new List<ConnectionConfig> {
new ConnectionConfig()
{
ConnectionString = connectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConfigId = "main"
},
new ConnectionConfig()
{
ConnectionString = logConn,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConfigId = "log"
},
new ConnectionConfig()
{
ConnectionString = SFCData,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConfigId = "data"
},
new ConnectionConfig()
{
ConnectionString = SFCAddOn,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConfigId = "addon"
}
});
//调式代码 用来打印SQL
db.Aop.OnLogExecuting = (sql, pars) =>
{
Debug.WriteLine(sql + "\r\n" +
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
}
/// <summary>
/// 获取所有
/// </summary>
/// <returns></returns>
public virtual List<T> GetList()
{
try
{
return CurrentDb.GetList();
}
catch (SqlSugarException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 根据主键获取单条数据
/// </summary>
/// <param name="Pk"></param>
/// <returns></returns>
public virtual T GetSingle(int Pk)
{
try
{
if (Pk <= 0)
return null;
return CurrentDb.GetById(Pk);
}
catch (SqlSugarException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 根据主键删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual bool Delete(dynamic id)
{
try
{
return CurrentDb.Delete(id);
}
catch (SqlSugarException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual bool Update(T obj)
{
try
{
return CurrentDb.Update(obj);
}
catch (SqlSugarException)
{
throw;
}
catch (Exception)
{
throw;
}
}
}
}