From 03e67bd6141177bb563e0320b939afcfec6e4ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Tue, 27 Sep 2022 09:39:18 +0800 Subject: [PATCH] =?UTF-8?q?SQLITE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmart.Model/BPASmart.Model.csproj | 1 + BPASmart.Model/{ => Config}/BinConfigFile.cs | 0 .../{ => Config}/FileConfigModel.cs | 0 BPASmart.Model/{ => Config}/GlobalVar.cs | 0 BPASmart.Model/{ => Config}/LocalPar.cs | 0 BPASmart.Model/{ => Config}/ProjectModel.cs | 0 BPASmart.Model/IRecipeMaterials.cs | 19 +++++ BPASmart.Model/配方/RecipeMaterials.cs | 3 + BPASmart.Server/SqliteContext.cs | 72 +++++++++++++++++++ BPASmart.Server/SqliteHelper.cs | 30 -------- 10 files changed, 95 insertions(+), 30 deletions(-) rename BPASmart.Model/{ => Config}/BinConfigFile.cs (100%) rename BPASmart.Model/{ => Config}/FileConfigModel.cs (100%) rename BPASmart.Model/{ => Config}/GlobalVar.cs (100%) rename BPASmart.Model/{ => Config}/LocalPar.cs (100%) rename BPASmart.Model/{ => Config}/ProjectModel.cs (100%) create mode 100644 BPASmart.Model/IRecipeMaterials.cs create mode 100644 BPASmart.Server/SqliteContext.cs delete mode 100644 BPASmart.Server/SqliteHelper.cs diff --git a/BPASmart.Model/BPASmart.Model.csproj b/BPASmart.Model/BPASmart.Model.csproj index 92946c2e..30051f2e 100644 --- a/BPASmart.Model/BPASmart.Model.csproj +++ b/BPASmart.Model/BPASmart.Model.csproj @@ -7,6 +7,7 @@ + diff --git a/BPASmart.Model/BinConfigFile.cs b/BPASmart.Model/Config/BinConfigFile.cs similarity index 100% rename from BPASmart.Model/BinConfigFile.cs rename to BPASmart.Model/Config/BinConfigFile.cs diff --git a/BPASmart.Model/FileConfigModel.cs b/BPASmart.Model/Config/FileConfigModel.cs similarity index 100% rename from BPASmart.Model/FileConfigModel.cs rename to BPASmart.Model/Config/FileConfigModel.cs diff --git a/BPASmart.Model/GlobalVar.cs b/BPASmart.Model/Config/GlobalVar.cs similarity index 100% rename from BPASmart.Model/GlobalVar.cs rename to BPASmart.Model/Config/GlobalVar.cs diff --git a/BPASmart.Model/LocalPar.cs b/BPASmart.Model/Config/LocalPar.cs similarity index 100% rename from BPASmart.Model/LocalPar.cs rename to BPASmart.Model/Config/LocalPar.cs diff --git a/BPASmart.Model/ProjectModel.cs b/BPASmart.Model/Config/ProjectModel.cs similarity index 100% rename from BPASmart.Model/ProjectModel.cs rename to BPASmart.Model/Config/ProjectModel.cs diff --git a/BPASmart.Model/IRecipeMaterials.cs b/BPASmart.Model/IRecipeMaterials.cs new file mode 100644 index 00000000..6cb23f2c --- /dev/null +++ b/BPASmart.Model/IRecipeMaterials.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BPASmart.Model; + +namespace BPASmart.Model +{ + public interface IRecipeMaterials + { + Task AddAsync(RecipeMaterials recipeMaterials); + Task AddRangeAsync(RecipeMaterials[] recipeMaterials); + Task UpdateAsync(RecipeMaterials recipeMaterials); + Task DeleteAsync(string ID); + Task> GetData(); + } +} diff --git a/BPASmart.Model/配方/RecipeMaterials.cs b/BPASmart.Model/配方/RecipeMaterials.cs index a50ce693..c7cdac30 100644 --- a/BPASmart.Model/配方/RecipeMaterials.cs +++ b/BPASmart.Model/配方/RecipeMaterials.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -28,6 +29,7 @@ namespace BPASmart.Model /// /// 物料重量 /// + [NotMapped] public int MaterialWeight { get { return _materialWeight; } set { _materialWeight = value; OnPropertyChanged(); } } private int _materialWeight; /// @@ -39,6 +41,7 @@ namespace BPASmart.Model /// /// 自定义原料属性集合 /// + [NotMapped] public ObservableCollection PropertyCollections = new ObservableCollection(); } diff --git a/BPASmart.Server/SqliteContext.cs b/BPASmart.Server/SqliteContext.cs new file mode 100644 index 00000000..bfb081d6 --- /dev/null +++ b/BPASmart.Server/SqliteContext.cs @@ -0,0 +1,72 @@ +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(); + })); + } + } +} diff --git a/BPASmart.Server/SqliteHelper.cs b/BPASmart.Server/SqliteHelper.cs deleted file mode 100644 index 9c3f604a..00000000 --- a/BPASmart.Server/SqliteHelper.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using BPASmart.Model; - -namespace BPASmart.Server -{ - public class SqliteHelper : DbContext - { - - string path - { - get - { - Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB")); - return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\DB\\RecipeInfo.db"; - } - } - - //public DbSet<> - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseSqlite($"FileName={path}"); - } - } -}