DbContext.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace DbCommon.BusinessCore.DbCore
  10. {
  11. public class DbContext<T> : DbContextBase<T> where T : class, new()
  12. {
  13. public DbContext()
  14. {
  15. Db = new SqlSugarClient(new ConnectionConfig()
  16. {
  17. ConnectionString = Function.Config.Instance.WmsConnString,
  18. DbType = DbType.SqlServer,
  19. InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
  20. IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
  21. });
  22. //调式代码 用来打印SQL
  23. //Db.Aop.OnLogExecuting = (sql, p) =>
  24. //{
  25. // Console.WriteLine(sql);
  26. // Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
  27. //};
  28. Db.Aop.OnError = (e) =>
  29. {
  30. string strLog = string.Format("Err: {0}\r\n{1}", e.Message, e.StackTrace);
  31. System.Diagnostics.Trace.WriteLine(strLog);
  32. var p = e.Parametres as SugarParameter[];
  33. string prams = string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value));
  34. string sqlLog = string.Format("SqlErr: {0}\r\n{1}", e.Sql, prams);
  35. System.Diagnostics.Trace.WriteLine(sqlLog);
  36. };
  37. }
  38. }
  39. }