using Microsoft.EntityFrameworkCore; using Elwig.Models; using System.Linq; using System.Threading.Tasks; namespace Elwig.Helpers { public class AppDbContext : DbContext { public DbSet Countries { get; set; } public DbSet Currencies { get; set; } public DbSet Members { get; set; } public DbSet BillingAddresses { get; set; } public DbSet Gemeinden { get; set; } public DbSet Katastralgemeinden { get; set; } public DbSet Orte { get; set; } public DbSet Postleitzahlen { get; set; } public DbSet PlzDestinations { get; set; } public DbSet PostalDestinations { get; set; } public DbSet Branches { get; set; } public DbSet WbKgs { get; set; } public DbSet WbRde { get; set; } public DbSet AreaCommitments { get; set; } public DbSet AreaCommitmentParcels { get; set; } public DbSet AreaCommitmentAttributes { get; set; } public DbSet Contracts { get; set; } public DbSet WineAttributes { get; set; } public DbSet WineCultivations { get; set; } public DbSet WineQualities { get; set; } public DbSet WineVarieties { get; set; } public DbSet Seasons { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite($"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default"); optionsBuilder.UseLazyLoadingProxies(); base.OnConfiguring(optionsBuilder); } public async Task MgNrExists(int mgnr) { return await Members.FindAsync(mgnr) != null; } public async Task VNrExists(int vnr) { return await Contracts.FindAsync(vnr) != null; } public async Task NextMgNr() { int c = await Members.Select(m => m.MgNr).MinAsync(); (await Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToListAsync()) .ForEach(a => { if (a <= c + 100) c = a; }); return c + 1; } public async Task NextVNr() { int c = await Contracts.Select(co => co.VNr).MinAsync(); (await Contracts.OrderBy(co => co.VNr).Select(co => co.VNr).ToListAsync()) .ForEach(a => { if (a <= c + 100) c = a; }); return c + 1; } } }