25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.6 KiB

  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace DataVAPI.ProcessServices.文件存储
  11. {
  12. public class SqlContext<T> : DbContext where T : DataVModel, new()
  13. {
  14. public static SqlContext<T> Base = new();
  15. public SqlContext()
  16. {
  17. }
  18. public static string path
  19. {
  20. get
  21. {
  22. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile"));
  23. return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{typeof(T).Name}.db";
  24. }
  25. }
  26. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  27. {
  28. optionsBuilder.UseSqlite($"FileName={path}");
  29. }
  30. private static readonly object SqliteLock = new object();
  31. public DbSet<T> DbObj { get; set; }
  32. /// <summary>
  33. /// 保存数据
  34. /// </summary>
  35. public void Save()
  36. {
  37. Database.EnsureCreated();
  38. SaveChanges();
  39. }
  40. /// <summary>
  41. /// 添加或者更新
  42. /// </summary>
  43. /// <param name="obj"></param>
  44. public void AddToUpdat(T obj)
  45. {
  46. lock (SqliteLock)
  47. {
  48. var result = DbObj.FirstOrDefault(t => t.Id.Equals(obj.Id));
  49. if (result != null) result.json = obj.json;
  50. else DbObj.Add(obj);
  51. Database.EnsureCreated();
  52. SaveChanges();
  53. }
  54. }
  55. /// <summary>
  56. /// 添加记录
  57. /// </summary>
  58. /// <param name="obj"></param>
  59. /// <returns></returns>
  60. public void Add(T obj)
  61. {
  62. lock (SqliteLock)
  63. {
  64. DbObj.Add(obj);
  65. }
  66. }
  67. /// <summary>
  68. /// 获取信息
  69. /// </summary>
  70. /// <param name="ID"></param>
  71. /// <returns></returns>
  72. public T GetInfo(string ID)
  73. {
  74. lock (SqliteLock)
  75. {
  76. Database.EnsureCreated();
  77. return DbObj.FirstOrDefault(t => t.Id.Equals(ID));
  78. }
  79. }
  80. /// <summary>
  81. /// 获取数据集合
  82. /// </summary>
  83. /// <returns></returns>
  84. public List<T> GetLists()
  85. {
  86. lock (SqliteLock)
  87. {
  88. Database.EnsureCreated();
  89. return DbObj.ToList();
  90. }
  91. }
  92. /// <summary>
  93. /// 删除信息
  94. /// </summary>
  95. /// <param name="orderID"></param>
  96. /// <returns></returns>
  97. public void Remove(string ID)
  98. {
  99. lock (SqliteLock)
  100. {
  101. var result = DbObj.FirstOrDefault(t => t.Id.Equals(ID));
  102. if (result != null)
  103. {
  104. Remove(result);
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 更新信息
  110. /// </summary>
  111. /// <param name="obj"></param>
  112. /// <returns></returns>
  113. public void Update(T obj)
  114. {
  115. lock (SqliteLock)
  116. {
  117. var result = DbObj.FirstOrDefault(t => t.Id.Equals(obj.Id));
  118. if (result != null) result.json = obj.json;
  119. }
  120. }
  121. }
  122. public class DataVModel
  123. {
  124. [Key]
  125. public string Id { get; set; }
  126. public string json { get; set; }
  127. }
  128. }