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

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