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

50 rivejä
1.2 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 void Save()
  30. {
  31. Database.EnsureCreated();
  32. var a = SaveChanges();
  33. }
  34. public List<T> GetData()
  35. {
  36. Database.EnsureCreated();
  37. return Base.ToList();
  38. }
  39. }
  40. }