IwbDbContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Abp.EntityFramework;
  6. using IwbZero.Authorization.Permissions;
  7. using IwbZero.Authorization.Roles;
  8. using IwbZero.Authorization.Users;
  9. using IwbZero.BaseSysInfo;
  10. namespace IwbZero
  11. {
  12. public abstract class IwbDbContext<TRole, TUser> : AbpDbContext
  13. where TRole : IwbSysRole<TUser>,new()
  14. where TUser : IwbSysUser<TUser>,new()
  15. //where TFun: IwbSysFunction<TUser>,new()
  16. {
  17. //TODO: Define an IDbSet for each Entity...
  18. public virtual IDbSet<TUser> Users { get; set; }
  19. public virtual IDbSet<UserLogin> UserLogins { get; set; }
  20. public virtual IDbSet<UserLoginAttempt> UserLoginLogs { get; set; }
  21. public virtual IDbSet<TRole> Roles { get; set; }
  22. public virtual IDbSet<SysUserRole> UserRoles { get; set; }
  23. public virtual IDbSet<SysPermission> Permissions { get; set; }
  24. //public virtual IDbSet<TFun> Functions { get; set; }
  25. //public virtual IDbSet<IwbSysSetting<TUser>> Settings { get; set; }
  26. //public virtual IDbSet<IwbSysState<TUser>> SysStates { get; set; }
  27. //Example:
  28. //public virtual IDbSet<User> Users { get; set; }
  29. /* NOTE:
  30. * Setting "Default" to base class helps us when working migration commands on Package Manager Console.
  31. * But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
  32. * pass connection string name to base classes. ABP works either way.
  33. */
  34. /// <summary>
  35. /// Default constructor.
  36. /// Do not directly instantiate this class. Instead, use dependency injection!
  37. /// </summary>
  38. protected IwbDbContext()
  39. {
  40. }
  41. /// <summary>
  42. /// Constructor with connection string parameter.
  43. /// </summary>
  44. /// <param name="nameOrConnectionString">Connection string or a name in connection strings in configuration file</param>
  45. protected IwbDbContext(string nameOrConnectionString)
  46. : base(nameOrConnectionString)
  47. {
  48. }
  49. protected IwbDbContext(DbCompiledModel model)
  50. : base(model)
  51. {
  52. }
  53. /// <summary>
  54. /// This constructor can be used for unit tests.
  55. /// </summary>
  56. protected IwbDbContext(DbConnection existingConnection, bool contextOwnsConnection)
  57. : base(existingConnection, contextOwnsConnection)
  58. {
  59. }
  60. protected IwbDbContext(string nameOrConnectionString, DbCompiledModel model)
  61. : base(nameOrConnectionString, model)
  62. {
  63. }
  64. protected IwbDbContext(ObjectContext objectContext, bool dbContextOwnsObjectContext)
  65. : base(objectContext, dbContextOwnsObjectContext)
  66. {
  67. }
  68. /// <summary>
  69. /// Constructor.
  70. /// </summary>
  71. protected IwbDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
  72. : base(existingConnection, model, contextOwnsConnection)
  73. {
  74. }
  75. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  76. {
  77. base.OnModelCreating(modelBuilder);
  78. #region IwbSysLog.Set_MaxLengths
  79. modelBuilder.Entity<IwbSysLog>()
  80. .Property(e => e.ServiceName)
  81. .HasMaxLength(IwbSysLog.MaxServiceNameLength);
  82. modelBuilder.Entity<IwbSysLog>()
  83. .Property(e => e.MethodName)
  84. .HasMaxLength(IwbSysLog.MaxMethodNameLength);
  85. modelBuilder.Entity<IwbSysLog>()
  86. .Property(e => e.Parameters)
  87. .HasMaxLength(IwbSysLog.MaxParametersLength);
  88. modelBuilder.Entity<IwbSysLog>()
  89. .Property(e => e.ClientIpAddress)
  90. .HasMaxLength(IwbSysLog.MaxClientIpAddressLength);
  91. modelBuilder.Entity<IwbSysLog>()
  92. .Property(e => e.ClientName)
  93. .HasMaxLength(IwbSysLog.MaxClientNameLength);
  94. modelBuilder.Entity<IwbSysLog>()
  95. .Property(e => e.BrowserInfo)
  96. .HasMaxLength(IwbSysLog.MaxBrowserInfoLength);
  97. modelBuilder.Entity<IwbSysLog>()
  98. .Property(e => e.Exception)
  99. .HasMaxLength(IwbSysLog.MaxExceptionLength);
  100. modelBuilder.Entity<IwbSysLog>()
  101. .Property(e => e.CustomData)
  102. .HasMaxLength(IwbSysLog.MaxCustomDataLength);
  103. #endregion
  104. }
  105. }
  106. }