VzHostDbContext.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Abp.BackgroundJobs;
  2. using Abp.Notifications;
  3. using Microsoft.EntityFrameworkCore;
  4. using VberZero.BaseSystem.MultiTenancy;
  5. using VberZero.BaseSystem.Users;
  6. namespace VberZero.EntityFramework;
  7. [Abp.MultiTenancy.MultiTenancySide(Abp.MultiTenancy.MultiTenancySides.Host)]
  8. public abstract class VzHostDbContext<TSelf> : VzCommonDbContext<TSelf>
  9. where TSelf : VzHostDbContext<TSelf>
  10. {
  11. /// <summary>
  12. /// Tenants
  13. /// </summary>
  14. public virtual DbSet<Tenant> Tenants { get; set; }
  15. ///// <summary>
  16. ///// Editions.
  17. ///// </summary>
  18. //public virtual DbSet<Edition> Editions { get; set; }
  19. ///// <summary>
  20. ///// FeatureSettings.
  21. ///// </summary>
  22. //public virtual DbSet<FeatureSetting> FeatureSettings { get; set; }
  23. ///// <summary>
  24. ///// TenantFeatureSetting.
  25. ///// </summary>
  26. //public virtual DbSet<TenantFeatureSetting> TenantFeatureSettings { get; set; }
  27. ///// <summary>
  28. ///// EditionFeatureSettings.
  29. ///// </summary>
  30. //public virtual DbSet<EditionFeatureSetting> EditionFeatureSettings { get; set; }
  31. public virtual DbSet<BackgroundJobInfo> BackgroundJobs { get; set; }
  32. public virtual DbSet<UserAccount> UserAccounts { get; set; }
  33. public virtual DbSet<NotificationInfo> Notifications { get; set; }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="options"></param>
  38. protected VzHostDbContext(DbContextOptions<TSelf> options)
  39. : base(options)
  40. {
  41. }
  42. protected override void OnModelCreating(ModelBuilder modelBuilder)
  43. {
  44. base.OnModelCreating(modelBuilder);
  45. modelBuilder.Entity<Tenant>(b =>
  46. {
  47. b.HasOne(p => p.DeleterUser)
  48. .WithMany()
  49. .HasForeignKey(p => p.DeleterUserId);
  50. b.HasOne(p => p.CreatorUser)
  51. .WithMany()
  52. .HasForeignKey(p => p.CreatorUserId);
  53. b.HasOne(p => p.LastModifierUser)
  54. .WithMany()
  55. .HasForeignKey(p => p.LastModifierUserId);
  56. b.HasIndex(e => e.TenancyName);
  57. });
  58. modelBuilder.Entity<BackgroundJobInfo>(b =>
  59. {
  60. b.HasIndex(e => new { e.IsAbandoned, e.NextTryTime });
  61. });
  62. modelBuilder.Entity<UserAccount>(b =>
  63. {
  64. b.HasIndex(e => new { e.TenantId, e.UserId });
  65. b.HasIndex(e => new { e.TenantId, e.UserName });
  66. b.HasIndex(e => new { e.TenantId, e.EmailAddress });
  67. b.HasIndex(e => new { e.UserName });
  68. b.HasIndex(e => new { e.EmailAddress });
  69. });
  70. //modelBuilder.Entity<TenantFeatureSetting>(b =>
  71. //{
  72. // b.HasIndex(e => new { e.TenantId, e.Name });
  73. //});
  74. //modelBuilder.Entity<EditionFeatureSetting>(b =>
  75. //{
  76. // b.HasIndex(e => new { e.EditionId, e.Name });
  77. //});
  78. }
  79. }