using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPASmart.Model; using System.Collections.ObjectModel; namespace BPASmart.Server { public class SqliteContext : DbContext, IRecipeMaterials { private volatile static SqliteContext _Instance; public static SqliteContext GetInstance => _Instance ?? (_Instance = new SqliteContext()); private SqliteContext() { } string path { get { Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB")); return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\RecipeInfo.db"; } } public DbSet RecipeMaterialsS { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite($"FileName={path}"); } public async Task AddAsync(RecipeMaterials recipeMaterials) { Database.EnsureCreated(); await RecipeMaterialsS.AddAsync(recipeMaterials); return await SaveChangesAsync() > 1; } public async Task AddRangeAsync(RecipeMaterials[] recipeMaterials) { Database.EnsureCreated(); await RecipeMaterialsS.AddRangeAsync(recipeMaterials); return await SaveChangesAsync() > 1; } public async Task UpdateAsync(RecipeMaterials recipeMaterials) { Database.EnsureCreated(); RecipeMaterialsS.Update(recipeMaterials); return await SaveChangesAsync() > 1; } public async Task DeleteAsync(string ID) { Database.EnsureCreated(); RecipeMaterialsS.Remove(RecipeMaterialsS.FirstOrDefault(p => p.ID == ID)); return await SaveChangesAsync() > 1; } public async Task> GetData() { return await Task.Factory.StartNew(new Func>(() => { Database.EnsureCreated(); return RecipeMaterialsS.ToList(); })); } } }