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.

43 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. static string Route()
  27. {
  28. var dir = SyncStatic.CreateDir(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));
  29. var r = SyncStatic.CreateFile(Path.Combine(dir, "data.db3"));
  30. return r;
  31. }
  32. public static void InitTable()
  33. {
  34. var Tables = Assembly.GetAssembly(typeof(DbContext))
  35. .GetTypes().Where(t => t.BaseType == typeof(BaseEntity)).ToArray();
  36. Context.CodeFirst.InitTables(Tables);
  37. }
  38. }
  39. }