API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DataContext.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Microsoft.EntityFrameworkCore;
  2. using System.Data.SqlClient;
  3. using System.Linq;
  4. using UnivateProperties_API.Model;
  5. using UnivateProperties_API.Model.Banks;
  6. using UnivateProperties_API.Model.Campaigns;
  7. using UnivateProperties_API.Model.Communication;
  8. using UnivateProperties_API.Model.Financial;
  9. using UnivateProperties_API.Model.Logging;
  10. using UnivateProperties_API.Model.Misc;
  11. using UnivateProperties_API.Model.ProcessFlow;
  12. using UnivateProperties_API.Model.Properties;
  13. using UnivateProperties_API.Model.Region;
  14. using UnivateProperties_API.Model.Timeshare;
  15. using UnivateProperties_API.Model.Users;
  16. namespace UnivateProperties_API.Context
  17. {
  18. public class DataContext : DbContext
  19. {
  20. private string connectionString = "";
  21. internal object BidItem;
  22. public DataContext(DbContextOptions<DataContext> options) : base(options)
  23. {
  24. foreach (var extention in options.Extensions)
  25. {
  26. if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension")
  27. {
  28. //connectionString = ((SqlServerOptionsExtension)extention).ConnectionString;
  29. connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString;
  30. }
  31. }
  32. }
  33. public DataContext()
  34. {
  35. }
  36. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  37. {
  38. optionsBuilder.EnableSensitiveDataLogging();
  39. }
  40. #region User
  41. public virtual DbSet<Agency> Agencies { get; set; }
  42. public virtual DbSet<Agent> Agents { get; set; }
  43. public virtual DbSet<User> Users { get; set; }
  44. public virtual DbSet<Individual> Individuals { get; set; }
  45. public virtual DbSet<Address> Addresses { get; set; }
  46. public virtual DbSet<UserRole> Roles { get; set; }
  47. public virtual DbSet<NonRegIndividual> NonRegIndividuals { get; set; }
  48. #endregion User
  49. #region Communication
  50. public virtual DbSet<Email> Emails { get; set; }
  51. public virtual DbSet<SMTPAccount> Accounts { get; set; }
  52. public virtual DbSet<SMTPHost> Hosts { get; set; }
  53. public virtual DbSet<Template> Templates { get; set; }
  54. public virtual DbSet<PlaceHolder> PlaceHolders { get; set; }
  55. public virtual DbSet<MailRecipient> MailRecipients { get; set; }
  56. public virtual DbSet<MailModel> CommunicationLog { get; set; }
  57. public virtual DbSet<MailSource> MailSources { get; set; }
  58. #endregion Communication
  59. #region Property
  60. public DbSet<Property> Properties { get; set; }
  61. public DbSet<PropertyImage> PropertyImages { get; set; }
  62. public DbSet<PropertyType> PropertyTypes { get; set; }
  63. public DbSet<PropertyUserField> PropertyUserFields { get; set; }
  64. public DbSet<UserDefinedField> UserDefinedFields { get; set; }
  65. public DbSet<UserDefinedGroup> UserDefinedGroups { get; set; }
  66. #endregion
  67. #region Region
  68. public DbSet<Province> Provinces { get; set; }
  69. public DbSet<City> Cities { get; set; }
  70. public DbSet<Suburb> Suburbs { get; set; }
  71. #endregion
  72. #region Timeshare
  73. public DbSet<TimeshareWeek> Weeks { get; set; }
  74. public DbSet<Status> Status { get; set; }
  75. public DbSet<UnitConfiguration> UnitConfigurations { get; set; }
  76. public DbSet<UnitConfigurationType> UnitConfigurationTypes { get; set; }
  77. public DbSet<Season> Seasons { get; set; }
  78. public DbSet<Bank> Banks { get; set; }
  79. public DbSet<BankAccount> BankAccounts { get; set; }
  80. #endregion Timeshare
  81. #region ProcessFlow
  82. public DbSet<ProcessFlow> ProcessFlows { get; set; }
  83. public DbSet<BidItem> BidItems { get; set; }
  84. #endregion
  85. #region Logs
  86. public DbSet<SearchLog> SearchLogs { get; set; }
  87. #endregion
  88. #region Misc
  89. public DbSet<Location> Location { get; set; }
  90. public DbSet<Carousel> Carousel { get; set; }
  91. public DbSet<PlaceHolderFormat> PlaceHolderFormats { get; set; }
  92. public DbSet<Default> Defaults { get; set; }
  93. public DbSet<TC> TermsConditions { get; set; }
  94. #endregion
  95. #region Payments
  96. public DbSet<Payment> Payments { get; set; }
  97. public DbSet<ListingFee> ListingFees { get; set; }
  98. #endregion
  99. #region Campaign
  100. public DbSet<Campaign> Campaigns { get; set; }
  101. public DbSet<CampaignItem> CampaignItems { get; set; }
  102. public DbSet<CampaignPlaceHolder> CampaignPlaceHolders { get; set; }
  103. public DbSet<CampaignItemPlaceHolder> CampaignItemPlaceHolders { get; set; }
  104. public DbSet<UploadCampaign> CampaignUploads { get; set; }
  105. #endregion
  106. public override int SaveChanges()
  107. {
  108. foreach (var item in ChangeTracker
  109. .Entries()
  110. .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added)
  111. .Select(x => x.Entity)
  112. .ToList())
  113. {
  114. if (item is BaseEntity)
  115. {
  116. (item as BaseEntity).UpdateModified(string.Empty);
  117. }
  118. }
  119. UpdateSoftDeleteStatuses();
  120. return base.SaveChanges();
  121. }
  122. private void UpdateSoftDeleteStatuses()
  123. {
  124. foreach (var entry in ChangeTracker.Entries())
  125. {
  126. switch (entry.State)
  127. {
  128. case EntityState.Added:
  129. entry.CurrentValues["IsDeleted"] = false;
  130. break;
  131. case EntityState.Deleted:
  132. entry.State = EntityState.Modified;
  133. entry.CurrentValues["IsDeleted"] = true;
  134. break;
  135. }
  136. }
  137. }
  138. protected override void OnModelCreating(ModelBuilder modelBuilder)
  139. {
  140. modelBuilder.Entity<Individual>()
  141. .Ignore(b => b.FullName);
  142. modelBuilder.Entity<SMTPHost>().ToTable("Hosts");
  143. modelBuilder.Entity<UnitConfiguration>()
  144. .HasIndex(u => u.Code)
  145. .IsUnique();
  146. modelBuilder.Entity<Individual>(b =>
  147. {
  148. b.HasKey(e => e.Id);
  149. b.Property(e => e.Id).ValueGeneratedOnAdd();
  150. });
  151. modelBuilder.Entity<Individual>()
  152. .HasIndex(i => new { i.Telephone, i.CellNumber, i.Email })
  153. .IsUnique(true);
  154. modelBuilder.Entity<Agent>(b =>
  155. {
  156. b.HasKey(e => e.Id);
  157. b.Property(e => e.Id).ValueGeneratedOnAdd();
  158. });
  159. modelBuilder.Entity<Agency>(b =>
  160. {
  161. b.HasKey(e => e.Id);
  162. b.Property(e => e.Id).ValueGeneratedOnAdd();
  163. });
  164. modelBuilder.Entity<Person>(b =>
  165. {
  166. b.HasKey(e => e.Id);
  167. b.Property(e => e.Id).ValueGeneratedOnAdd();
  168. });
  169. modelBuilder.Entity<User>()
  170. .HasIndex(u => u.Username)
  171. .IsUnique(true);
  172. modelBuilder.Entity<Email>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  173. modelBuilder.Entity<SMTPAccount>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  174. modelBuilder.Entity<SMTPHost>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  175. modelBuilder.Entity<Property>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  176. modelBuilder.Entity<PropertyImage>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  177. modelBuilder.Entity<PropertyType>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  178. modelBuilder.Entity<PropertyUserField>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  179. modelBuilder.Entity<UserDefinedField>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  180. modelBuilder.Entity<UserDefinedGroup>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  181. modelBuilder.Entity<City>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  182. modelBuilder.Entity<Province>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  183. modelBuilder.Entity<Suburb>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  184. modelBuilder.Entity<Season>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  185. modelBuilder.Entity<Status>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  186. modelBuilder.Entity<TimeshareWeek>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  187. modelBuilder.Entity<UnitConfiguration>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  188. modelBuilder.Entity<UnitConfigurationType>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  189. modelBuilder.Entity<Agency>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  190. modelBuilder.Entity<Person>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  191. modelBuilder.Entity<User>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  192. modelBuilder.Entity<Bank>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  193. modelBuilder.Entity<BankAccount>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  194. modelBuilder.Entity<SearchLog>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  195. modelBuilder.Entity<Address>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  196. modelBuilder.Entity<BidItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  197. modelBuilder.Entity<ProcessFlow>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  198. modelBuilder.Entity<Template>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  199. modelBuilder.Entity<Carousel>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  200. modelBuilder.Entity<PlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  201. modelBuilder.Entity<Campaign>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  202. modelBuilder.Entity<CampaignItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  203. modelBuilder.Entity<CampaignItemPlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  204. modelBuilder.Entity<CampaignPlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  205. modelBuilder.Entity<PlaceHolderFormat>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  206. modelBuilder.Entity<MailSource>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
  207. }
  208. public int GetMaxID(string tableName)
  209. {
  210. SqlConnection connection = new SqlConnection(connectionString);
  211. connection.Open();
  212. SqlCommand cmd = connection.CreateCommand();
  213. cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
  214. SqlDataReader reader = cmd.ExecuteReader();
  215. int returnValue = 0;
  216. while (reader.Read())
  217. {
  218. returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
  219. }
  220. connection.Close();
  221. return returnValue;
  222. }
  223. }
  224. }