Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DBContext.cs 5.0KB

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