Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2024
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | Help | 0 0
  1. var securityConnectionString = ConfigurationManager.Instance.GetRequiredSection("ConnectionStrings")["Security"];
  2. var serverVersion = ServerVersion.AutoDetect(securityConnectionString);
  3.  
  4. services.AddDbContextPool<Security.ApplicationLayer.Context.AccessTokenContext>(options =>
  5.     options.UseMySql(securityConnectionString, serverVersion)
  6. );
  7.  
  8. services.AddDbContextPool<Security.ApplicationLayer.Context.UserContext>(options =>
  9.      options.UseMySql(securityConnectionString, serverVersion)
  10. );
  11.  
  12. services.AddDbContextPool<Security.ApplicationLayer.Context.UserGroupContext>(options =>
  13.      options.UseMySql(securityConnectionString, serverVersion)
  14. );
  15.  
  16. services.AddDbContextPool<Security.ApplicationLayer.Context.PermissionContext>(options =>
  17.      options.UseMySql(securityConnectionString, serverVersion)
  18. );
  19.  
  20. services.AddDbContextPool<Security.ApplicationLayer.Context.RoleContext>(options =>
  21.      options.UseMySql(securityConnectionString, serverVersion)
  22. );
  23.  
  24. var serviceProvider = services.BuildServiceProvider();
  25. var userContext = serviceProvider.GetService<Security.ApplicationLayer.Context.UserContext>();
  26.  
  27. var accessTokenContext = serviceProvider.GetService<Security.ApplicationLayer.Context.AccessTokenContext>();
  28. var userGroupContext = serviceProvider.GetService<Security.ApplicationLayer.Context.UserGroupContext>();
  29. var permissionContext = serviceProvider.GetService<Security.ApplicationLayer.Context.PermissionContext>();
  30. var roleContext = serviceProvider.GetService<Security.ApplicationLayer.Context.RoleContext>();
  31.  
  32. accessTokenContext.Database.EnsureCreated();
  33. userContext.Database.EnsureCreated();
  34. userGroupContext.Database.EnsureCreated();
  35. permissionContext.Database.EnsureCreated();
  36. roleContext.Database.EnsureCreated();
  37.  
  38. public class UserContext : DbContext
  39. {
  40.     public DbSet<User> User { get; set; }
  41.     public DbSet<UserCredential> UserCredential { get; set; }
  42.  
  43.     public UserContext(DbContextOptions<UserContext> options) : base(options)
  44.     {
  45.         Database.EnsureCreated();
  46.     }
  47. }
  48.  
  49. public class AccessTokenContext : DbContext
  50. {
  51.     public DbSet<AccessToken> AccessToken { get; set; }
  52.  
  53.     public AccessTokenContext(DbContextOptions<AccessTokenContext> options) : base(options)
  54.     {
  55.         Database.EnsureCreated();
  56.     }
  57. }
  58.  
  59. public class UserGroupContext : DbContext
  60. {
  61.     public DbSet<UserGroup> UserGroup { get; set; }
  62.     public DbSet<UserInUserGroup> UserInUserGroup { get; set; }
  63.     public DbSet<RoleInUserGroup> RoleInUserGroup { get; set; }
  64.  
  65.     public UserGroupContext(DbContextOptions<UserGroupContext> options) : base(options)
  66.     {
  67.         Database.EnsureCreated();
  68.     }
  69. }
  70.  
  71. public class PermissionContext : DbContext
  72. {
  73.     public DbSet<Permission> Permission { get; set; }
  74.  
  75.     public PermissionContext(DbContextOptions<PermissionContext> options) : base(options)
  76.     {
  77.         Database.EnsureCreated();
  78.     }
  79. }
  80.  
  81. public class RoleContext : DbContext
  82. {
  83.     public DbSet<Role> Role { get; set; }
  84.     public DbSet<PermissionInRole> PermissionInRole { get; set; }
  85.  
  86.     public RoleContext(DbContextOptions<RoleContext> options) : base(options)
  87.     {
  88.         Database.EnsureCreated();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement