DataSource.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Vit.Core.Util.ConfigurationManager;
  2. using Vitorm.Sql;
  3. namespace Vitorm.MsTest
  4. {
  5. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  6. public class User
  7. {
  8. [System.ComponentModel.DataAnnotations.Key]
  9. [System.ComponentModel.DataAnnotations.Schema.Column("userId")]
  10. public int id { get; set; }
  11. [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
  12. public string name { get; set; }
  13. public DateTime? birth { get; set; }
  14. public int? fatherId { get; set; }
  15. public int? motherId { get; set; }
  16. public int? classId { get; set; }
  17. public static User NewUser(int id, bool forAdd = false) => new User { id = id, name = "testUser" + id };
  18. public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
  19. {
  20. return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
  21. }
  22. }
  23. public class UserClass
  24. {
  25. [System.ComponentModel.DataAnnotations.Key]
  26. public int id { get; set; }
  27. public string name { get; set; }
  28. public static List<UserClass> NewClasses(int startId, int count = 1)
  29. {
  30. return Enumerable.Range(startId, count).Select(id => new UserClass { id = id, name = "class" + id }).ToList();
  31. }
  32. }
  33. public class DataSource
  34. {
  35. public static void WaitForUpdate() { }
  36. readonly static string connectionString = Appsettings.json.GetStringByPath("Vitorm.PostgreSQL.connectionString");
  37. public static SqlDbContext CreateDbContextForWriting(bool autoInit = true)
  38. {
  39. var dbContext = new SqlDbContext();
  40. dbContext.UsePostgreSQL(connectionString);
  41. dbContext.ChangeDatabase(dbContext.databaseName + "2");
  42. if (autoInit) InitDbContext(dbContext);
  43. return dbContext;
  44. }
  45. static bool initedDefaultIndex = false;
  46. public static SqlDbContext CreateDbContext()
  47. {
  48. var dbContext = new SqlDbContext();
  49. dbContext.UsePostgreSQL(connectionString);
  50. lock (typeof(DataSource))
  51. {
  52. if (!initedDefaultIndex)
  53. {
  54. InitDbContext(dbContext);
  55. initedDefaultIndex = true;
  56. }
  57. }
  58. return dbContext;
  59. }
  60. static void InitDbContext(SqlDbContext dbContext)
  61. {
  62. #region #1 init User
  63. {
  64. dbContext.TryDropTable<User>();
  65. dbContext.TryCreateTable<User>();
  66. var users = new List<User> {
  67. new User { id=1, name="u146", fatherId=4, motherId=6 },
  68. new User { id=2, name="u246", fatherId=4, motherId=6 },
  69. new User { id=3, name="u356", fatherId=5, motherId=6 },
  70. new User { id=4, name="u400" },
  71. new User { id=5, name="u500" },
  72. new User { id=6, name="u600" },
  73. };
  74. users.ForEach(user =>
  75. {
  76. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  77. user.classId = user.id % 2 + 1;
  78. });
  79. dbContext.AddRange(users);
  80. }
  81. #endregion
  82. #region #2 init Class
  83. {
  84. dbContext.TryDropTable<UserClass>();
  85. dbContext.TryCreateTable<UserClass>();
  86. dbContext.AddRange(UserClass.NewClasses(1, 6));
  87. }
  88. #endregion
  89. WaitForUpdate();
  90. }
  91. }
  92. }