|
|
@@ -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<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(); |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
} |