DataSource.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Vit.Core.Util.ConfigurationManager;
  2. using Vitorm.Sql;
  3. namespace Vitorm.MsTest
  4. {
  5. [System.ComponentModel.DataAnnotations.Schema.Table("User", Schema = "dbo")]
  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() => CreateDbContext();
  50. public static SqlDbContext CreateDbContext()
  51. {
  52. var dbContext = new SqlDbContext();
  53. dbContext.UseSqlServer(connectionString);
  54. dbContext.BeginTransaction();
  55. #region #1 init User
  56. {
  57. dbContext.Drop<User>();
  58. dbContext.Create<User>();
  59. var users = new List<User> {
  60. new User { name="u146", fatherId=4, motherId=6 },
  61. new User { name="u246", fatherId=4, motherId=6 },
  62. new User { name="u356", fatherId=5, motherId=6 },
  63. new User { name="u400" },
  64. new User { name="u500" },
  65. new User { name="u600" },
  66. };
  67. dbContext.AddRange(users);
  68. users.ForEach(user =>
  69. {
  70. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  71. user.classId = user.id % 2 + 1;
  72. });
  73. dbContext.UpdateRange(users);
  74. }
  75. #endregion
  76. #region #2 init Class
  77. {
  78. dbContext.Drop<UserClass>();
  79. dbContext.Create<UserClass>();
  80. dbContext.AddRange(UserClass.NewClasses(1, 6));
  81. }
  82. #endregion
  83. return dbContext;
  84. }
  85. }
  86. }