DataSource.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Vitorm.Sql;
  2. using Vit.Extensions;
  3. using Vit.Core.Util.ConfigurationManager;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. namespace Vitorm.MsTest
  6. {
  7. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  8. public class User
  9. {
  10. [System.ComponentModel.DataAnnotations.Key]
  11. [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  12. public int id { get; set; }
  13. public string name { get; set; }
  14. public DateTime? birth { get; set; }
  15. public int? fatherId { get; set; }
  16. public int? motherId { get; set; }
  17. }
  18. public class DataSource
  19. {
  20. static string connectionString = Appsettings.json.GetStringByPath("App.Db.ConnectionString");
  21. public static SqlDbContext CreateDbContext()
  22. {
  23. var dbContext = new SqlDbContext();
  24. dbContext.UseMySql(connectionString);
  25. dbContext.BeginTransaction();
  26. var userSet = dbContext.DbSet<User>();
  27. dbContext.Execute(sql: "DROP TABLE if exists `User`;");
  28. userSet.Create();
  29. var users = new List<User> {
  30. new User { name="u146", fatherId=4, motherId=6 },
  31. new User { name="u246", fatherId=4, motherId=6 },
  32. new User { name="u356", fatherId=5, motherId=6 },
  33. new User { name="u400" },
  34. new User { name="u500" },
  35. new User { name="u600" },
  36. };
  37. dbContext.AddRange(users);
  38. users.ForEach(user => { user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id); });
  39. dbContext.UpdateRange(users);
  40. return dbContext;
  41. }
  42. }
  43. }