|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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<RecipeMaterials> RecipeMaterialsS { get; set; }
-
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlite($"FileName={path}");
- }
-
- public async Task<bool> AddAsync(RecipeMaterials recipeMaterials)
- {
- Database.EnsureCreated();
- await RecipeMaterialsS.AddAsync(recipeMaterials);
- return await SaveChangesAsync() > 1;
- }
-
- public async Task<bool> AddRangeAsync(RecipeMaterials[] recipeMaterials)
- {
- Database.EnsureCreated();
- await RecipeMaterialsS.AddRangeAsync(recipeMaterials);
- return await SaveChangesAsync() > 1;
- }
-
- public async Task<bool> UpdateAsync(RecipeMaterials recipeMaterials)
- {
- Database.EnsureCreated();
- RecipeMaterialsS.Update(recipeMaterials);
- return await SaveChangesAsync() > 1;
- }
-
- public async Task<bool> DeleteAsync(string ID)
- {
- Database.EnsureCreated();
- RecipeMaterialsS.Remove(RecipeMaterialsS.FirstOrDefault(p => p.ID == ID));
- return await SaveChangesAsync() > 1;
- }
-
- public async Task<List<RecipeMaterials>> GetData()
- {
- return await Task.Factory.StartNew(new Func<List<RecipeMaterials>>(() =>
- {
- Database.EnsureCreated();
- return RecipeMaterialsS.ToList();
- }));
- }
- }
- }
|