终端一体化运控平台
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.
 
 
 

57 lines
1.5 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 BPASmartClient.Helper
  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. int Year = DateTime.Now.Year;
  26. int Month = DateTime.Now.Month;
  27. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB\\{Year}-{Month}"));
  28. return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\{Year}-{Month}\\{typeof(T).Name}.db";
  29. }
  30. }
  31. public bool DataBaseExist()
  32. {
  33. return Database.EnsureCreated();
  34. }
  35. public void Save()
  36. {
  37. Database.EnsureCreated();
  38. var a = SaveChanges();
  39. }
  40. public List<T> GetData()
  41. {
  42. Database.EnsureCreated();
  43. return Base.ToList();
  44. }
  45. }
  46. }