|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel.DataAnnotations;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace DataVAPI.ProcessServices.文件存储
- {
- public class SqlContext<T> : DbContext where T : DataVModel, new()
- {
- public static SqlContext<T> Base = new();
-
- public SqlContext()
- {
-
- }
-
- public static string path
- {
- get
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile"));
- return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{typeof(T).Name}.db";
- }
- }
-
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlite($"FileName={path}");
- }
-
- private static readonly object SqliteLock = new object();
-
- public DbSet<T> DbObj { get; set; }
-
- /// <summary>
- /// 保存数据
- /// </summary>
- public void Save()
- {
- Database.EnsureCreated();
- SaveChanges();
- }
-
- /// <summary>
- /// 添加或者更新
- /// </summary>
- /// <param name="obj"></param>
- public void AddToUpdat(T obj)
- {
- lock (SqliteLock)
- {
- var result = DbObj.FirstOrDefault(t => t.Id.Equals(obj.Id));
- if (result != null) result.json = obj.json;
- else DbObj.Add(obj);
- Database.EnsureCreated();
- SaveChanges();
- }
- }
- /// <summary>
- /// 添加记录
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public void Add(T obj)
- {
- lock (SqliteLock)
- {
- DbObj.Add(obj);
- }
- }
-
- /// <summary>
- /// 获取信息
- /// </summary>
- /// <param name="ID"></param>
- /// <returns></returns>
- public T GetInfo(string ID)
- {
- lock (SqliteLock)
- {
- Database.EnsureCreated();
- return DbObj.FirstOrDefault(t => t.Id.Equals(ID));
- }
-
- }
-
- /// <summary>
- /// 获取数据集合
- /// </summary>
- /// <returns></returns>
- public List<T> GetLists()
- {
- lock (SqliteLock)
- {
- Database.EnsureCreated();
- return DbObj.ToList();
- }
- }
-
-
- /// <summary>
- /// 删除信息
- /// </summary>
- /// <param name="orderID"></param>
- /// <returns></returns>
- public void Remove(string ID)
- {
- lock (SqliteLock)
- {
- var result = DbObj.FirstOrDefault(t => t.Id.Equals(ID));
- if (result != null)
- {
- Remove(result);
- }
- }
- }
-
- /// <summary>
- /// 更新信息
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public void Update(T obj)
- {
- lock (SqliteLock)
- {
- var result = DbObj.FirstOrDefault(t => t.Id.Equals(obj.Id));
- if (result != null) result.json = obj.json;
- }
- }
- }
-
- public class DataVModel
- {
- [Key]
- public string Id { get; set; }
- public string json { get; set; }
- }
- }
|