终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SqliteContext.cs 2.3 KiB

vor 2 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BPASmart.Model;
  8. using System.Collections.ObjectModel;
  9. namespace BPASmart.Server
  10. {
  11. public class SqliteContext : DbContext, IRecipeMaterials
  12. {
  13. private volatile static SqliteContext _Instance;
  14. public static SqliteContext GetInstance => _Instance ?? (_Instance = new SqliteContext());
  15. private SqliteContext() { }
  16. string path
  17. {
  18. get
  19. {
  20. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB"));
  21. return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\RecipeInfo.db";
  22. }
  23. }
  24. public DbSet<RecipeMaterials> RecipeMaterialsS { get; set; }
  25. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  26. {
  27. optionsBuilder.UseSqlite($"FileName={path}");
  28. }
  29. public async Task<bool> AddAsync(RecipeMaterials recipeMaterials)
  30. {
  31. Database.EnsureCreated();
  32. await RecipeMaterialsS.AddAsync(recipeMaterials);
  33. return await SaveChangesAsync() > 1;
  34. }
  35. public async Task<bool> AddRangeAsync(RecipeMaterials[] recipeMaterials)
  36. {
  37. Database.EnsureCreated();
  38. await RecipeMaterialsS.AddRangeAsync(recipeMaterials);
  39. return await SaveChangesAsync() > 1;
  40. }
  41. public async Task<bool> UpdateAsync(RecipeMaterials recipeMaterials)
  42. {
  43. Database.EnsureCreated();
  44. RecipeMaterialsS.Update(recipeMaterials);
  45. return await SaveChangesAsync() > 1;
  46. }
  47. public async Task<bool> DeleteAsync(string ID)
  48. {
  49. Database.EnsureCreated();
  50. RecipeMaterialsS.Remove(RecipeMaterialsS.FirstOrDefault(p => p.ID == ID));
  51. return await SaveChangesAsync() > 1;
  52. }
  53. public async Task<List<RecipeMaterials>> GetData()
  54. {
  55. return await Task.Factory.StartNew(new Func<List<RecipeMaterials>>(() =>
  56. {
  57. Database.EnsureCreated();
  58. return RecipeMaterialsS.ToList();
  59. }));
  60. }
  61. }
  62. }