DataSource.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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) => CreateDbContext(autoInit);
  38. public static SqlDbContext CreateDbContext(bool autoInit = true)
  39. {
  40. var dbContext = new SqlDbContext();
  41. dbContext.UsePostgreSQL(connectionString);
  42. if (autoInit) InitDbContext(dbContext);
  43. return dbContext;
  44. }
  45. static void InitDbContext(SqlDbContext dbContext)
  46. {
  47. #region #1 init User
  48. {
  49. dbContext.TryDropTable<User>();
  50. dbContext.TryCreateTable<User>();
  51. var users = new List<User> {
  52. new User { id=1, name="u146", fatherId=4, motherId=6 },
  53. new User { id=2, name="u246", fatherId=4, motherId=6 },
  54. new User { id=3, name="u356", fatherId=5, motherId=6 },
  55. new User { id=4, name="u400" },
  56. new User { id=5, name="u500" },
  57. new User { id=6, name="u600" },
  58. };
  59. users.ForEach(user =>
  60. {
  61. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  62. user.classId = user.id % 2 + 1;
  63. });
  64. dbContext.AddRange(users);
  65. }
  66. #endregion
  67. #region #2 init Class
  68. {
  69. dbContext.TryDropTable<UserClass>();
  70. dbContext.TryCreateTable<UserClass>();
  71. dbContext.AddRange(UserClass.NewClasses(1, 6));
  72. }
  73. #endregion
  74. WaitForUpdate();
  75. }
  76. }
  77. }