IwbZeroDbContext.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System.Data.Common;
  2. using System.Data.Entity;
  3. using System.Data.Entity.Core.Objects;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Data.Entity.ModelConfiguration.Conventions;
  6. using Abp.Domain.Entities;
  7. using Abp.EntityFramework.Extensions;
  8. using Abp.Notifications;
  9. using IwbZero.Authorization.Base.SystemInfo;
  10. using IwbZero.Authorization.Base.Users;
  11. using IwbZero.Authorization.Roles;
  12. using IwbZero.Authorization.Users;
  13. using IwbZero.MultiTenancy;
  14. namespace IwbZero.EntityFramework
  15. {
  16. public abstract class IwbZeroDbContext<TTenant, TRole, TUser> : IwbZeroCommonDbContext<TRole, TUser>
  17. where TTenant : IwbTenant<TUser>
  18. where TRole : IwbSysRole<TUser>
  19. where TUser : IwbSysUser<TUser>
  20. {
  21. /// <summary>
  22. /// Tenants
  23. /// </summary>
  24. public virtual IDbSet<TTenant> Tenants { get; set; }
  25. ///// <summary>
  26. ///// Editions.
  27. ///// </summary>
  28. //public virtual IDbSet<Edition> Editions { get; set; }
  29. ///// <summary>
  30. ///// FeatureSettings.
  31. ///// </summary>
  32. //public virtual IDbSet<FeatureSetting> FeatureSettings { get; set; }
  33. ///// <summary>
  34. ///// TenantFeatureSetting.
  35. ///// </summary>
  36. //public virtual IDbSet<TenantFeatureSetting> TenantFeatureSettings { get; set; }
  37. ///// <summary>
  38. ///// EditionFeatureSettings.
  39. ///// </summary>
  40. //public virtual IDbSet<EditionFeatureSetting> EditionFeatureSettings { get; set; }
  41. ///// <summary>
  42. ///// Background jobs.
  43. ///// </summary>
  44. //public virtual IDbSet<BackgroundJobInfo> BackgroundJobs { get; set; }
  45. /// <summary>
  46. /// User accounts
  47. /// </summary>
  48. public virtual IDbSet<UserAccount> UserAccounts { get; set; }
  49. protected IwbZeroDbContext()
  50. {
  51. }
  52. protected IwbZeroDbContext(string nameOrConnectionString)
  53. : base(nameOrConnectionString)
  54. {
  55. }
  56. protected IwbZeroDbContext(DbCompiledModel model)
  57. : base(model)
  58. {
  59. }
  60. protected IwbZeroDbContext(DbConnection existingConnection, bool contextOwnsConnection)
  61. : base(existingConnection, contextOwnsConnection)
  62. {
  63. }
  64. protected IwbZeroDbContext(string nameOrConnectionString, DbCompiledModel model)
  65. : base(nameOrConnectionString, model)
  66. {
  67. }
  68. protected IwbZeroDbContext(ObjectContext objectContext, bool dbContextOwnsObjectContext)
  69. : base(objectContext, dbContextOwnsObjectContext)
  70. {
  71. }
  72. protected IwbZeroDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
  73. : base(existingConnection, model, contextOwnsConnection)
  74. {
  75. }
  76. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  77. {
  78. base.OnModelCreating(modelBuilder);
  79. //#region BackgroundJobInfo.IX_IsAbandoned_NextTryTime
  80. //modelBuilder.Entity<BackgroundJobInfo>()
  81. // .Property(j => j.IsAbandoned)
  82. // .CreateIndex("IX_IsAbandoned_NextTryTime", 1);
  83. //modelBuilder.Entity<BackgroundJobInfo>()
  84. // .Property(j => j.NextTryTime)
  85. // .CreateIndex("IX_IsAbandoned_NextTryTime", 2);
  86. //#endregion
  87. #region NotificationSubscriptionInfo.IX_NotificationName_EntityTypeName_EntityId_UserId
  88. modelBuilder.Entity<NotificationSubscriptionInfo>()
  89. .Property(ns => ns.NotificationName)
  90. .CreateIndex("IX_NotificationName_EntityTypeName_EntityId_UserId", 1);
  91. modelBuilder.Entity<NotificationSubscriptionInfo>()
  92. .Property(ns => ns.EntityTypeName)
  93. .CreateIndex("IX_NotificationName_EntityTypeName_EntityId_UserId", 2);
  94. modelBuilder.Entity<NotificationSubscriptionInfo>()
  95. .Property(ns => ns.EntityId)
  96. .CreateIndex("IX_NotificationName_EntityTypeName_EntityId_UserId", 3);
  97. modelBuilder.Entity<NotificationSubscriptionInfo>()
  98. .Property(ns => ns.UserId)
  99. .CreateIndex("IX_NotificationName_EntityTypeName_EntityId_UserId", 4);
  100. #endregion
  101. #region UserNotificationInfo.IX_UserId_State_CreationTime
  102. modelBuilder.Entity<UserNotificationInfo>()
  103. .Property(un => un.UserId)
  104. .CreateIndex("IX_UserId_State_CreationTime", 1);
  105. modelBuilder.Entity<UserNotificationInfo>()
  106. .Property(un => un.State)
  107. .CreateIndex("IX_UserId_State_CreationTime", 2);
  108. modelBuilder.Entity<UserNotificationInfo>()
  109. .Property(un => un.CreationTime)
  110. .CreateIndex("IX_UserId_State_CreationTime", 3);
  111. #endregion
  112. #region UserLoginAttempt.IX_TenancyName_UserNameOrEmailAddress_Result
  113. modelBuilder.Entity<UserLoginAttempt>()
  114. .Property(ula => ula.TenancyName)
  115. .CreateIndex("IX_TenancyName_UserNameOrEmailAddress_Result", 1);
  116. modelBuilder.Entity<UserLoginAttempt>()
  117. .Property(ula => ula.UserNameOrEmailAddress)
  118. .CreateIndex("IX_TenancyName_UserNameOrEmailAddress_Result", 2);
  119. modelBuilder.Entity<UserLoginAttempt>()
  120. .Property(ula => ula.Result)
  121. .CreateIndex("IX_TenancyName_UserNameOrEmailAddress_Result", 3);
  122. #endregion
  123. #region UserLoginAttempt.IX_UserId_TenantId
  124. modelBuilder.Entity<UserLoginAttempt>()
  125. .Property(ula => ula.UserId)
  126. .CreateIndex("IX_UserId_TenantId", 1);
  127. modelBuilder.Entity<UserLoginAttempt>()
  128. .Property(ula => ula.TenantId)
  129. .CreateIndex("IX_UserId_TenantId", 2);
  130. #endregion
  131. #region SysLog.Set_MaxLengths
  132. modelBuilder.Entity<SysLog>()
  133. .Property(e => e.ServiceName)
  134. .HasMaxLength(SysLog.MaxServiceNameLength);
  135. modelBuilder.Entity<SysLog>()
  136. .Property(e => e.MethodName)
  137. .HasMaxLength(SysLog.MaxMethodNameLength);
  138. modelBuilder.Entity<SysLog>()
  139. .Property(e => e.Parameters)
  140. .HasMaxLength(SysLog.MaxParametersLength);
  141. modelBuilder.Entity<SysLog>()
  142. .Property(e => e.ClientIpAddress)
  143. .HasMaxLength(SysLog.MaxClientIpAddressLength);
  144. modelBuilder.Entity<SysLog>()
  145. .Property(e => e.ClientName)
  146. .HasMaxLength(SysLog.MaxClientNameLength);
  147. modelBuilder.Entity<SysLog>()
  148. .Property(e => e.BrowserInfo)
  149. .HasMaxLength(SysLog.MaxBrowserInfoLength);
  150. modelBuilder.Entity<SysLog>()
  151. .Property(e => e.Exception)
  152. .HasMaxLength(SysLog.MaxExceptionLength);
  153. modelBuilder.Entity<SysLog>()
  154. .Property(e => e.CustomData)
  155. .HasMaxLength(SysLog.MaxCustomDataLength);
  156. #endregion
  157. #region Common Indexes
  158. modelBuilder.Conventions.AddAfter<IndexAttributeConvention>(new IndexingPropertyConvention<ISoftDelete, bool>(m => m.IsDeleted));
  159. modelBuilder.Conventions.AddAfter<IndexAttributeConvention>(new IndexingPropertyConvention<IMayHaveTenant, int?>(m => m.TenantId));
  160. modelBuilder.Conventions.AddAfter<IndexAttributeConvention>(new IndexingPropertyConvention<IMustHaveTenant, int>(m => m.TenantId));
  161. #endregion
  162. }
  163. }
  164. }