You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DBContext.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Microsoft.CodeAnalysis;
  2. using Microsoft.EntityFrameworkCore;
  3. using Npgsql;
  4. using ProRestaurant.Models;
  5. using ProRestaurant.Models.Accounts;
  6. using ProRestaurant.Models.Misc;
  7. using ProRestaurant.Models.Restaurants;
  8. using System.Data.SqlClient;
  9. using System.Linq;
  10. namespace ProRestaurant.DBContexts
  11. {
  12. public class DBContext : DbContext
  13. {
  14. private string connectionString = "";
  15. private bool UseSQL = false;
  16. public DBContext(DbContextOptions<DBContext> options)
  17. : base(options)
  18. {
  19. foreach (var extention in options.Extensions)
  20. {
  21. if (extention.GetType().ToString() == "Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension")
  22. {
  23. connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString;
  24. UseSQL = false;
  25. }
  26. if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension")
  27. {
  28. connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString;
  29. UseSQL = true;
  30. }
  31. }
  32. }
  33. #region Accounts
  34. public DbSet<User> Users { get; set; }
  35. public DbSet<DriverDetail> DriverDetails { get; set; }
  36. public DbSet<UserAddress> UserAddresses { get; set; }
  37. #endregion
  38. #region Restaurants
  39. public DbSet<Restaurant> Restaurants { get; set; }
  40. public DbSet<TradingHours> TradingHours { get; set; }
  41. public DbSet<MenuCategory> MenuCategories { get; set; }
  42. public DbSet<MenuItem> MenuItems { get; set; }
  43. public DbSet<RestaurantUser> RestaurantUsers { get; set; }
  44. public DbSet<RestaurantCategory> RestaurantCategories { get; set; }
  45. #endregion
  46. #region Misc
  47. public DbSet<Locations> Locations { get; set; }
  48. #endregion
  49. public override int SaveChanges()
  50. {
  51. bool isBaseObject = false;
  52. foreach (var item in ChangeTracker
  53. .Entries()
  54. .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added)
  55. .Select(x => x.Entity)
  56. .ToList())
  57. {
  58. if (item is BaseObject)
  59. {
  60. isBaseObject = true;
  61. (item as BaseObject).UpdateModified(string.Empty);
  62. }
  63. }
  64. if (isBaseObject)
  65. UpdateSoftDeleteStatuses();
  66. return base.SaveChanges();
  67. }
  68. private void UpdateSoftDeleteStatuses()
  69. {
  70. foreach (var entry in ChangeTracker.Entries())
  71. {
  72. switch (entry.State)
  73. {
  74. case EntityState.Added:
  75. entry.CurrentValues["IsDeleted"] = false;
  76. break;
  77. case EntityState.Deleted:
  78. entry.State = EntityState.Modified;
  79. entry.CurrentValues["IsDeleted"] = true;
  80. break;
  81. }
  82. }
  83. }
  84. protected override void OnModelCreating(ModelBuilder modelBuilder)
  85. {
  86. modelBuilder.Entity<User>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  87. modelBuilder.Entity<UserAddress>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  88. modelBuilder.Entity<DriverDetail>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  89. modelBuilder.Entity<Restaurant>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  90. modelBuilder.Entity<TradingHours>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  91. modelBuilder.Entity<MenuCategory>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  92. modelBuilder.Entity<MenuItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  93. modelBuilder.Entity<RestaurantUser>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  94. }
  95. public int GetNextID(string tableName)
  96. {
  97. int returnValue = 0;
  98. if (!UseSQL)
  99. {
  100. NpgsqlConnection connection = new NpgsqlConnection(connectionString);
  101. connection.Open();
  102. NpgsqlCommand cmd = connection.CreateCommand();
  103. cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
  104. NpgsqlDataReader reader = cmd.ExecuteReader();
  105. while (reader.Read())
  106. {
  107. returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
  108. }
  109. connection.Close();
  110. }
  111. else
  112. {
  113. SqlConnection connection = new SqlConnection(connectionString);
  114. connection.Open();
  115. SqlCommand cmd = connection.CreateCommand();
  116. cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
  117. SqlDataReader reader = cmd.ExecuteReader();
  118. while (reader.Read())
  119. {
  120. returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
  121. }
  122. connection.Close();
  123. }
  124. return returnValue + 1;
  125. }
  126. }
  127. }