Runner_EntityFramework.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Microsoft.EntityFrameworkCore;
  2. using Vit.Core.Util.ConfigurationManager;
  3. using Vitorm;
  4. namespace App.OrmRunner
  5. {
  6. public partial class Runner_EntityFramework : IRunner
  7. {
  8. RunConfig config;
  9. int? skip => config.skip;
  10. int take => config.take;
  11. bool executeQuery => config.executeQuery;
  12. IQueryable<User> userQuery;
  13. public IQueryable<User> GetQueryable() => userQuery;
  14. public void Run(RunConfig config)
  15. {
  16. this.config = config;
  17. for (int i = 0; i < config.repeatCount; i++)
  18. {
  19. using MyDbContext myDbContext = new MyDbContext();
  20. userQuery = myDbContext.users;
  21. if (config.queryJoin) QueryJoin();
  22. else Query();
  23. }
  24. }
  25. #region Executor
  26. int exceptUserId = 1;
  27. public void QueryJoin()
  28. {
  29. var userSet = GetQueryable();
  30. var minId = 1;
  31. var config = new { maxId = 10000 };
  32. var offsetId = 100;
  33. var query =
  34. from user in userSet
  35. from father in userSet.Where(father => user.fatherId == father.id).DefaultIfEmpty()
  36. from mother in userSet.Where(mother => user.motherId == mother.id).DefaultIfEmpty()
  37. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  38. orderby user.id
  39. select new
  40. {
  41. user,
  42. father,
  43. mother,
  44. testId = user.id + offsetId,
  45. hasFather = father.name != null ? true : false
  46. }
  47. ;
  48. Execute(query);
  49. }
  50. public void Query()
  51. {
  52. var userSet = GetQueryable();
  53. var minId = 1;
  54. var config = new { maxId = 10000 };
  55. var query =
  56. from user in userSet
  57. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  58. orderby user.id
  59. select user;
  60. Execute(query);
  61. }
  62. #endregion
  63. public void Execute<Result>(IQueryable<Result> query)
  64. {
  65. if (skip > 0) query = query.Skip(skip.Value);
  66. query = query.Take(take);
  67. if (executeQuery)
  68. {
  69. var userList = query.ToList();
  70. var rowCount = userList.Count;
  71. if (rowCount != take) throw new Exception($"query failed, expected row count : {take} , actual count: {rowCount} ");
  72. }
  73. else
  74. {
  75. var sql = query.ToQueryString();
  76. if (string.IsNullOrEmpty(sql)) throw new Exception($"query failed, can not generated sql script");
  77. }
  78. }
  79. public class MyDbContext : Microsoft.EntityFrameworkCore.DbContext
  80. {
  81. public Microsoft.EntityFrameworkCore.DbSet<User> users { get; set; }
  82. static string connectionString = Appsettings.json.GetStringByPath("Vitorm.Data[0].connectionString");
  83. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  84. {
  85. optionsBuilder.UseSqlite(connectionString);
  86. }
  87. }
  88. // Entity Definition
  89. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  90. public class User
  91. {
  92. [System.ComponentModel.DataAnnotations.Key]
  93. public int id { get; set; }
  94. public string name { get; set; }
  95. public DateTime? birth { get; set; }
  96. public int? fatherId { get; set; }
  97. public int? motherId { get; set; }
  98. }
  99. }
  100. }