FreeSqlDbContextExtensions.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using ZhonTai.Admin.Core.Repositories;
  3. using System.Linq.Expressions;
  4. using ZhonTai.Admin.Tools.TaskScheduler;
  5. using FreeScheduler;
  6. using ZhonTai.Admin.Core.Configs;
  7. namespace ZhonTai.Admin.Core.Db;
  8. public static class FreeSqlDbContextExtensions
  9. {
  10. /// <summary>
  11. /// 返回默认仓库类
  12. /// </summary>
  13. /// <typeparam name="TEntity"></typeparam>
  14. /// <typeparam name="TKey"></typeparam>
  15. /// <param name="that"></param>
  16. /// <param name="filter">数据过滤 + 验证</param>
  17. /// <returns></returns>
  18. public static IRepositoryBase<TEntity, TKey> GetRepositoryBase<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
  19. {
  20. return new RepositoryBase<TEntity, TKey>(that, filter);
  21. }
  22. /// <summary>
  23. /// 返回默认仓库类,适用联合主键的仓储类
  24. /// </summary>
  25. /// <typeparam name="TEntity"></typeparam>
  26. /// <param name="that"></param>
  27. /// <param name="filter">数据过滤 + 验证</param>
  28. /// <returns></returns>
  29. public static IRepositoryBase<TEntity, long> GetRepositoryBase<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
  30. {
  31. return new RepositoryBase<TEntity, long>(that, filter);
  32. }
  33. /// <summary>
  34. /// 同步调度结构
  35. /// </summary>
  36. /// <param name="that"></param>
  37. /// <param name="dbConfig"></param>
  38. /// <param name="configureFreeSql"></param>
  39. public static void SyncSchedulerStructure(this IFreeSql that, DbConfig dbConfig, Action<IFreeSql> configureFreeSql = null)
  40. {
  41. that.CodeFirst
  42. .ConfigEntity<TaskInfo>(a =>
  43. {
  44. a.Name("ad_task");
  45. a.Property(b => b.Id).IsPrimary(true);
  46. a.Property(b => b.Body).StringLength(-1);
  47. a.Property(b => b.Interval).MapType(typeof(int));
  48. a.Property(b => b.IntervalArgument).StringLength(1024);
  49. a.Property(b => b.Status).MapType(typeof(int));
  50. a.Property(b => b.CreateTime).ServerTime(DateTimeKind.Local);
  51. a.Property(b => b.LastRunTime).ServerTime(DateTimeKind.Local);
  52. })
  53. .ConfigEntity<TaskLog>(a =>
  54. {
  55. a.Name("ad_task_log");
  56. a.Property(b => b.Exception).StringLength(-1);
  57. a.Property(b => b.Remark).StringLength(-1);
  58. a.Property(b => b.CreateTime).ServerTime(DateTimeKind.Local);
  59. });
  60. configureFreeSql?.Invoke(that);
  61. if (dbConfig.SyncStructure)
  62. {
  63. that.CodeFirst.SyncStructure<TaskInfo>();
  64. that.CodeFirst.SyncStructure<TaskLog>();
  65. }
  66. }
  67. }