VzDbContext.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Abp.BackgroundJobs;
  2. using Abp.Notifications;
  3. using Microsoft.EntityFrameworkCore;
  4. using VberZero.BaseSystem;
  5. using VberZero.BaseSystem.MultiTenancy;
  6. using VberZero.BaseSystem.Users;
  7. using VberZero.Workflow;
  8. using VberZero.Workflow.DesignInfo;
  9. using VberZero.Workflow.Persistence;
  10. namespace VberZero.EntityFramework;
  11. /// <summary>
  12. /// Vber Zero的基础 DbContext。
  13. /// 从此类派生您的 DbContext 以拥有基本实体。
  14. /// </summary>
  15. public abstract class VzDbContext<TSelf> : VzCommonDbContext<TSelf>
  16. where TSelf : VzDbContext<TSelf>
  17. {
  18. /// <summary>
  19. /// Tenants
  20. /// </summary>
  21. public virtual DbSet<Tenant> Tenants { get; set; }
  22. public virtual DbSet<BackgroundJobInfo> BackgroundJobs { get; set; }
  23. public virtual DbSet<UserAccount> UserAccounts { get; set; }
  24. public virtual DbSet<NotificationInfo> Notifications { get; set; }
  25. #region WORKFLOW
  26. public DbSet<WorkflowEventInfo> WorkflowEvents { get; set; }
  27. public DbSet<WorkflowExecutionErrorInfo> WorkflowExecutionErrors { get; set; }
  28. public DbSet<WorkflowExecutionPointerInfo> WorkflowExecutionPointers { get; set; }
  29. public DbSet<WorkflowExtensionAttributeInfo> WorkflowExtensionAttributes { get; set; }
  30. public DbSet<WorkflowSubscriptionInfo> WorkflowSubscriptions { get; set; }
  31. public DbSet<WorkflowInfo> WorkflowWorkflows { get; set; }
  32. public DbSet<WorkflowDefinitionInfo> WorkflowWorkflowDefinitions { get; set; }
  33. public DbSet<CommonWorkflowAuditorInfo> CommonWorkflowAuditors { get; set; }
  34. #endregion WORKFLOW
  35. ///// <summary>
  36. ///// Editions.
  37. ///// </summary>
  38. //public virtual DbSet<Edition> Editions { get; set; }
  39. ///// <summary>
  40. ///// FeatureSettings.
  41. ///// </summary>
  42. //public virtual DbSet<FeatureSetting> FeatureSettings { get; set; }
  43. ///// <summary>
  44. ///// TenantFeatureSetting.
  45. ///// </summary>
  46. //public virtual DbSet<TenantFeatureSetting> TenantFeatureSettings { get; set; }
  47. ///// <summary>
  48. ///// EditionFeatureSettings.
  49. ///// </summary>
  50. //public virtual DbSet<EditionFeatureSetting> EditionFeatureSettings { get; set; }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. /// <param name="options"></param>
  55. protected VzDbContext(DbContextOptions<TSelf> options)
  56. : base(options)
  57. {
  58. }
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. /// <param name="modelBuilder"></param>
  63. protected override void OnModelCreating(ModelBuilder modelBuilder)
  64. {
  65. base.OnModelCreating(modelBuilder);
  66. modelBuilder.ChangeTablePrefix();
  67. modelBuilder.Entity<BackgroundJobInfo>(b =>
  68. {
  69. b.HasIndex(e => new { e.IsAbandoned, e.NextTryTime });
  70. });
  71. modelBuilder.Entity<Tenant>(b =>
  72. {
  73. b.HasOne(p => p.DeleterUser)
  74. .WithMany()
  75. .HasForeignKey(p => p.DeleterUserId);
  76. b.HasOne(p => p.CreatorUser)
  77. .WithMany()
  78. .HasForeignKey(p => p.CreatorUserId);
  79. b.HasOne(p => p.LastModifierUser)
  80. .WithMany()
  81. .HasForeignKey(p => p.LastModifierUserId);
  82. b.HasIndex(e => e.TenancyName);
  83. });
  84. modelBuilder.Entity<UserAccount>(b =>
  85. {
  86. b.HasIndex(e => new { e.TenantId, e.UserId });
  87. b.HasIndex(e => new { e.TenantId, e.UserName });
  88. b.HasIndex(e => new { e.TenantId, e.EmailAddress });
  89. b.HasIndex(e => new { e.UserName });
  90. b.HasIndex(e => new { e.EmailAddress });
  91. });
  92. #region AuditLog.Set_MaxLengths
  93. modelBuilder.Entity<AuditLog>()
  94. .Property(e => e.ServiceName)
  95. .HasMaxLength(AuditLog.MaxServiceNameLength);
  96. modelBuilder.Entity<AuditLog>()
  97. .Property(e => e.MethodName)
  98. .HasMaxLength(AuditLog.MaxMethodNameLength);
  99. modelBuilder.Entity<AuditLog>()
  100. .Property(e => e.Parameters)
  101. .HasMaxLength(AuditLog.MaxParametersLength);
  102. modelBuilder.Entity<AuditLog>()
  103. .Property(e => e.ClientIpAddress)
  104. .HasMaxLength(AuditLog.MaxClientIpAddressLength);
  105. modelBuilder.Entity<AuditLog>()
  106. .Property(e => e.ClientName)
  107. .HasMaxLength(AuditLog.MaxClientNameLength);
  108. modelBuilder.Entity<AuditLog>()
  109. .Property(e => e.BrowserInfo)
  110. .HasMaxLength(AuditLog.MaxBrowserInfoLength);
  111. modelBuilder.Entity<AuditLog>()
  112. .Property(e => e.ExceptionMessage)
  113. .HasMaxLength(AuditLog.MaxExceptionMessageLength);
  114. modelBuilder.Entity<AuditLog>()
  115. .Property(e => e.Exception)
  116. .HasMaxLength(AuditLog.MaxExceptionLength);
  117. modelBuilder.Entity<AuditLog>()
  118. .Property(e => e.CustomData)
  119. .HasMaxLength(AuditLog.MaxCustomDataLength);
  120. #endregion AuditLog.Set_MaxLengths
  121. //modelBuilder.Entity<TenantFeatureSetting>(b =>
  122. //{
  123. // b.HasIndex(e => new { e.TenantId, e.Name });
  124. //});
  125. //modelBuilder.Entity<EditionFeatureSetting>(b =>
  126. //{
  127. // b.HasIndex(e => new { e.EditionId, e.Name });
  128. //});
  129. modelBuilder.ConfigWorkflow();
  130. }
  131. }