DataSource.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using MongoDB.Bson;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. using Vit.Core.Util.ConfigurationManager;
  4. using Vitorm.MongoDB;
  5. namespace Vitorm.MsTest
  6. {
  7. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  8. public class User
  9. {
  10. public User() { }
  11. public User(int id) { this.id = id; }
  12. public User(string name) { this.name = name; }
  13. [System.ComponentModel.DataAnnotations.Key]
  14. [System.ComponentModel.DataAnnotations.Schema.Column("userId")]
  15. [BsonId]
  16. [BsonElement("userId")]
  17. //[BsonRepresentation(BsonType.Int32)]
  18. public int id { get; set; }
  19. [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
  20. [BsonElement("userName")]
  21. public string name { get; set; }
  22. //[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
  23. public DateTime? birth { get; set; }
  24. [System.ComponentModel.DataAnnotations.Schema.Column("userFatherId")]
  25. public int? fatherId { get; set; }
  26. [System.ComponentModel.DataAnnotations.Schema.Column("userMotherId")]
  27. public int? motherId { get; set; }
  28. [System.ComponentModel.DataAnnotations.Schema.Column("userClassId")]
  29. // [BsonIgnoreIfNull]
  30. public int? classId { get; set; }
  31. [System.ComponentModel.DataAnnotations.Schema.Column("userFather")]
  32. public User father { get; set; }
  33. [System.ComponentModel.DataAnnotations.Schema.NotMapped]
  34. [BsonIgnore]
  35. public string test { get; set; }
  36. public static User NewUser(int id, bool forAdd = false) => new User { id = id, name = "testUser" + id };
  37. public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
  38. {
  39. return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
  40. }
  41. }
  42. [System.ComponentModel.DataAnnotations.Schema.Table("UserClass")]
  43. public class UserClass
  44. {
  45. [System.ComponentModel.DataAnnotations.Key]
  46. [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
  47. [BsonId]
  48. [BsonElement("classId")]
  49. public int id { get; set; }
  50. [System.ComponentModel.DataAnnotations.Schema.Column("className")]
  51. [BsonElement("className")]
  52. public string name { get; set; }
  53. public static List<UserClass> NewClasses(int startId, int count = 1)
  54. {
  55. return Enumerable.Range(startId, count).Select(id => new UserClass { id = id, name = "class" + id }).ToList();
  56. }
  57. }
  58. public class DataSource
  59. {
  60. public static void WaitForUpdate() { }
  61. public static Vitorm.MongoDB.DbContext CreateDbContextForWriting(bool autoInit = true) => CreateDbContext(autoInit);
  62. public static Vitorm.MongoDB.DbContext CreateDbContext(bool autoInit = true)
  63. {
  64. var dbConfig = new DbConfig(Appsettings.json.GetByPath<Dictionary<string, object>>("Vitorm.MongoDB"));
  65. var dbContext = new Vitorm.MongoDB.DbContext(dbConfig);
  66. //dbContext.BeginTransaction();
  67. if (autoInit)
  68. InitDbContext(dbContext);
  69. return dbContext;
  70. }
  71. public static void InitDbContext(Vitorm.MongoDB.DbContext dbContext)
  72. {
  73. #region #1 init User
  74. {
  75. dbContext.TryDropTable<User>();
  76. dbContext.TryCreateTable<User>();
  77. var users = new List<User> {
  78. new User { id=1, name="u146", fatherId=4, motherId=6 },
  79. new User { id=2, name="u246", fatherId=4, motherId=6 },
  80. new User { id=3, name="u356", fatherId=5, motherId=6 },
  81. new User { id=4, name="u400" },
  82. new User { id=5, name="u500" },
  83. new User { id=6, name="u600" },
  84. };
  85. users.ForEach(user =>
  86. {
  87. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  88. user.classId = user.id % 2 + 1;
  89. });
  90. users.ForEach(user =>
  91. {
  92. user.father = users.FirstOrDefault(m => m.id == user.fatherId);
  93. });
  94. dbContext.AddRange(users);
  95. }
  96. #endregion
  97. #region #2 init Class
  98. {
  99. dbContext.TryDropTable<UserClass>();
  100. dbContext.TryCreateTable<UserClass>();
  101. dbContext.AddRange(UserClass.NewClasses(1, 6));
  102. }
  103. #endregion
  104. }
  105. }
  106. }