Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DBContext.cs 4.9KB

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