DataSource.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
  11. public int id { get; set; }
  12. [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
  13. public string name { get; set; }
  14. [System.ComponentModel.DataAnnotations.Schema.Column("userBirth")]
  15. public DateTime? birth { get; set; }
  16. [System.ComponentModel.DataAnnotations.Schema.Column("userFatherId")]
  17. public int? fatherId { get; set; }
  18. [System.ComponentModel.DataAnnotations.Schema.Column("userMotherId")]
  19. public int? motherId { get; set; }
  20. [System.ComponentModel.DataAnnotations.Schema.Column("userClassId")]
  21. public int? classId { get; set; }
  22. [System.ComponentModel.DataAnnotations.Schema.NotMapped]
  23. public string test { get; set; }
  24. public static User NewUser(int id, bool forAdd = false) => new User { id = forAdd ? 0 : id, name = "testUser" + id };
  25. public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
  26. {
  27. return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
  28. }
  29. }
  30. [System.ComponentModel.DataAnnotations.Schema.Table("UserClass")]
  31. public class UserClass
  32. {
  33. [System.ComponentModel.DataAnnotations.Key]
  34. [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
  35. [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
  36. public int id { get; set; }
  37. [System.ComponentModel.DataAnnotations.Schema.Column("className")]
  38. public string name { get; set; }
  39. public static List<UserClass> NewClasses(int startId, int count = 1)
  40. {
  41. return Enumerable.Range(startId, count).Select(id => new UserClass { id = 0, name = "class" + id }).ToList();
  42. }
  43. }
  44. public class DataSource
  45. {
  46. public static void WaitForUpdate() { }
  47. static string connectionString = Appsettings.json.GetStringByPath("Vitorm.MySql.connectionString");
  48. public static SqlDbContext CreateDbContextForWriting() => CreateDbContext();
  49. public static SqlDbContext CreateDbContext()
  50. {
  51. var dbContext = new SqlDbContext();
  52. dbContext.UseMySql(connectionString);
  53. dbContext.BeginTransaction();
  54. #region #1 init User
  55. {
  56. dbContext.Drop<User>();
  57. dbContext.Create<User>();
  58. var users = new List<User> {
  59. new User { name="u146", fatherId=4, motherId=6 },
  60. new User { name="u246", fatherId=4, motherId=6 },
  61. new User { name="u356", fatherId=5, motherId=6 },
  62. new User { name="u400" },
  63. new User { name="u500" },
  64. new User { name="u600" },
  65. };
  66. dbContext.AddRange(users);
  67. users.ForEach(user =>
  68. {
  69. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  70. user.classId = user.id % 2 + 1;
  71. });
  72. dbContext.UpdateRange(users);
  73. }
  74. #endregion
  75. #region #2 init Class
  76. {
  77. dbContext.Drop<UserClass>();
  78. dbContext.Create<UserClass>();
  79. dbContext.AddRange(UserClass.NewClasses(1, 6));
  80. }
  81. #endregion
  82. return dbContext;
  83. }
  84. }
  85. }