You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  1. using HKLib.DB.Model;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using XExten.Advance.StaticFramework;
  11. namespace HKLib.DB
  12. {
  13. public class DbContext
  14. {
  15. public static SqlSugarScope Context = new SqlSugarScope(new ConnectionConfig()
  16. {
  17. ConnectionString = Route,
  18. DbType = DbType.Sqlite,//数据库类型
  19. IsAutoCloseConnection = true //不设成true要手动close
  20. }, db =>
  21. {
  22. db.Aop.OnLogExecuting = (sql, pars) =>
  23. {
  24. };
  25. });
  26. public static string Route
  27. {
  28. get
  29. {
  30. var dir = SyncStatic.CreateDir(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));
  31. return $"DataSource={SyncStatic.CreateFile(Path.Combine(dir, "Data.db3"))}";
  32. }
  33. }
  34. public static void InitTable()
  35. {
  36. var Tables = Assembly.GetAssembly(typeof(DbContext))
  37. .GetTypes().Where(t => t.BaseType == typeof(BaseEntity)).ToArray();
  38. Context.CodeFirst.InitTables(Tables);
  39. }
  40. }
  41. }