DataSource.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
  10. [System.ComponentModel.DataAnnotations.Schema.Column("userId", TypeName = "int")]
  11. public int id { get; set; }
  12. [System.ComponentModel.DataAnnotations.Schema.Column("userName", TypeName = "varchar(1000)")]
  13. //[System.ComponentModel.DataAnnotations.Required]
  14. public string name { get; set; }
  15. [System.ComponentModel.DataAnnotations.Schema.Column("userBirth")]
  16. public DateTime? birth { get; set; }
  17. [System.ComponentModel.DataAnnotations.Schema.Column("userFatherId")]
  18. public int? fatherId { get; set; }
  19. [System.ComponentModel.DataAnnotations.Schema.Column("userMotherId")]
  20. public int? motherId { get; set; }
  21. [System.ComponentModel.DataAnnotations.Schema.Column("userClassId")]
  22. public int? classId { get; set; }
  23. [System.ComponentModel.DataAnnotations.Schema.NotMapped]
  24. public string test { get; set; }
  25. public static User NewUser(int id, bool forAdd = false) => new User { id = forAdd ? 0 : id, name = "testUser" + id };
  26. public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
  27. {
  28. return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
  29. }
  30. }
  31. [System.ComponentModel.DataAnnotations.Schema.Table("UserClass")]
  32. public class UserClass
  33. {
  34. [System.ComponentModel.DataAnnotations.Key]
  35. [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
  36. [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
  37. public int id { get; set; }
  38. [System.ComponentModel.DataAnnotations.Schema.Column("className")]
  39. public string name { get; set; }
  40. public static List<UserClass> NewClasses(int startId, int count = 1)
  41. {
  42. return Enumerable.Range(startId, count).Select(id => new UserClass { id = 0, name = "class" + id }).ToList();
  43. }
  44. }
  45. public class DataSource
  46. {
  47. public static void WaitForUpdate() { }
  48. static string connectionString = Appsettings.json.GetStringByPath("Vitorm.SqlServer.connectionString");
  49. public static SqlDbContext CreateDbContextForWriting(bool autoInit = true) => CreateDbContext(autoInit);
  50. public static SqlDbContext CreateDbContext(bool autoInit = true)
  51. {
  52. var dbContext = new SqlDbContext();
  53. dbContext.UseSqlServer(connectionString);
  54. //dbContext.BeginTransaction();
  55. if (autoInit)
  56. InitDbContext(dbContext);
  57. return dbContext;
  58. }
  59. public static void InitDbContext(SqlDbContext dbContext)
  60. {
  61. #region #1 init User
  62. {
  63. dbContext.TryDropTable<User>();
  64. dbContext.TryCreateTable<User>();
  65. var users = new List<User> {
  66. new User { name="u146", fatherId=4, motherId=6 },
  67. new User { name="u246", fatherId=4, motherId=6 },
  68. new User { name="u356", fatherId=5, motherId=6 },
  69. new User { name="u400" },
  70. new User { name="u500" },
  71. new User { name="u600" },
  72. };
  73. dbContext.AddRange(users);
  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.UpdateRange(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. }
  90. }
  91. }