using Microsoft.EntityFrameworkCore; using Npgsql; using ProRestaurant.Models; using ProRestaurant.Models.Accounts; using ProRestaurant.Models.Restaurants; using System.Data.SqlClient; using System.Linq; namespace ProRestaurant.DBContexts { public class DBContext : DbContext { private string connectionString = ""; private bool UseSQL = false; public DBContext(DbContextOptions options) : base(options) { foreach (var extention in options.Extensions) { if (extention.GetType().ToString() == "Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension") { connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString; UseSQL = false; } if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension") { connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString; UseSQL = true; } } } #region Accounts public DbSet Users { get; set; } public DbSet DriverDetails { get; set; } public DbSet UserAddresses { get; set; } #endregion #region Restaurants public DbSet Restaurants { get; set; } public DbSet TradingHours { get; set; } public DbSet MenuCategories { get; set; } public DbSet MenuItems { get; set; } #endregion public override int SaveChanges() { foreach (var item in ChangeTracker .Entries() .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added) .Select(x => x.Entity) .ToList()) { if (item is BaseObject) { (item as BaseObject).UpdateModified(string.Empty); } } UpdateSoftDeleteStatuses(); return base.SaveChanges(); } private void UpdateSoftDeleteStatuses() { foreach (var entry in ChangeTracker.Entries()) { switch (entry.State) { case EntityState.Added: entry.CurrentValues["IsDeleted"] = false; break; case EntityState.Deleted: entry.State = EntityState.Modified; entry.CurrentValues["IsDeleted"] = true; break; } } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false); modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false); modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false); modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false); modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false); } public int GetNextID(string tableName) { int returnValue = 0; if (!UseSQL) { NpgsqlConnection connection = new NpgsqlConnection(connectionString); connection.Open(); NpgsqlCommand cmd = connection.CreateCommand(); cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName); NpgsqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString()); } connection.Close(); } else { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString()); } connection.Close(); } return returnValue + 1; } } }