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.
 
 

59 lines
1.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.EntityFrameworkCore.Sqlite;
  9. namespace HBLConsole.Service
  10. {
  11. public class Sqlite<T> : DbContext where T : class, new()
  12. {
  13. private volatile static Sqlite<T> _Instance;
  14. public static Sqlite<T> GetInstance => _Instance ?? (_Instance = new Sqlite<T>());
  15. private Sqlite() { }
  16. public DbSet<T> Base { get; set; }
  17. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  18. {
  19. optionsBuilder.UseSqlite($"FileName={path}");
  20. }
  21. //static string path
  22. //{
  23. // get
  24. // {
  25. // Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\DB"));
  26. // return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\{typeof(T).Name}.db";
  27. // }
  28. //}
  29. static string path
  30. {
  31. get
  32. {
  33. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance.FilePath}\\DB"));
  34. return $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance.FilePath}\\DB\\{typeof(T).Name}.db";
  35. }
  36. }
  37. public void Save()
  38. {
  39. Database.EnsureCreated();
  40. SaveChanges();
  41. }
  42. public object GetData()
  43. {
  44. Database.EnsureCreated();
  45. return Base.ToList();
  46. }
  47. }
  48. }