|
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore.Sqlite;
-
- namespace BPASmartClient.Helper
- {
- public class Sqlite<T> : DbContext where T : class, new()
- {
-
- private volatile static Sqlite<T> _Instance;
- public static Sqlite<T> GetInstance => _Instance ?? (_Instance = new Sqlite<T>());
- private Sqlite() { }
-
- public DbSet<T> Base { get; set; }
-
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlite($"FileName={path}");
- }
-
- static string path
- {
- get
- {
- int Year = DateTime.Now.Year;
- int Month = DateTime.Now.Month;
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB\\{Year}-{Month}"));
- return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\{Year}-{Month}\\{typeof(T).Name}.db";
- }
- }
- public bool DataBaseExist()
- {
- return Database.EnsureCreated();
- }
-
- public void Save()
- {
- Database.EnsureCreated();
- var a = SaveChanges();
- }
-
- public List<T> GetData()
- {
- Database.EnsureCreated();
- return Base.ToList();
- }
-
-
-
- }
- }
|