DefaultDbContext.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Furion;
  2. using Furion.DatabaseAccessor;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore.Diagnostics;
  5. using Microsoft.Extensions.Configuration;
  6. using System;
  7. using System.Linq;
  8. using YBEE.EQM.Core;
  9. namespace YBEE.EQM.EntityFramework.Core
  10. {
  11. [AppDbContext("DbMain", DbProvider.MySql)]
  12. public class DefaultDbContext : AppDbContext<DefaultDbContext>
  13. {
  14. public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
  15. {
  16. // 启用实体数据更改监听
  17. EnabledEntityChangedListener = true;
  18. // 忽略空值更新
  19. InsertOrUpdateIgnoreNullValues = true;
  20. }
  21. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  22. {
  23. optionsBuilder.UseMySql(App.Configuration.GetConnectionString("DbMain"), new MySqlServerVersion(new Version(8, 0, 31))).UseSnakeCaseNamingConvention();
  24. }
  25. protected override void SavingChangesEvent(DbContextEventData eventData, InterceptionResult<int> result)
  26. {
  27. // 获取当前事件对应上下文
  28. var dbContext = eventData.Context;
  29. // 获取所有更改,删除,新增的实体,但排除审计实体(避免死循环)
  30. var entities = dbContext.ChangeTracker.Entries().Where(u =>
  31. u.Entity.GetType() != typeof(SysLogOp) &&
  32. u.Entity.GetType() != typeof(SysLogVis) &&
  33. u.Entity.GetType() != typeof(SysLogEx) &&
  34. (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added)
  35. ).ToList();
  36. if (entities == null || entities.Count < 1)
  37. {
  38. return;
  39. }
  40. // 当前操作者信息
  41. var userId = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  42. var userName = App.User?.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
  43. // 当前操作者机构信息
  44. var orgId = App.User?.FindFirst(ClaimConst.CLAINM_ORGID)?.Value;
  45. var orgName = App.User?.FindFirst(ClaimConst.CLAINM_ORGNAME)?.Value;
  46. foreach (var entity in entities)
  47. {
  48. if (entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase)) ||
  49. entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase<short>)) ||
  50. entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase<long>))
  51. )
  52. {
  53. dynamic obj;
  54. if (entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase<short>)))
  55. {
  56. obj = entity.Entity as DEntityBase<short>;
  57. }
  58. else if (entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase<long>)))
  59. {
  60. obj = entity.Entity as DEntityBase<long>;
  61. }
  62. else
  63. {
  64. obj = entity.Entity as DEntityBase;
  65. }
  66. if (entity.State == EntityState.Added)
  67. {
  68. obj.CreateTime = DateTime.Now;
  69. if (!string.IsNullOrEmpty(userId))
  70. {
  71. obj.CreateSysUserId = int.Parse(userId);
  72. }
  73. }
  74. else if (entity.State == EntityState.Modified)
  75. {
  76. // 排除创建人
  77. entity.Property(nameof(DEntityBase.CreateSysUserId)).IsModified = false;
  78. // 排除创建日期
  79. entity.Property(nameof(DEntityBase.CreateTime)).IsModified = false;
  80. obj.UpdateTime = DateTime.Now;
  81. if (!string.IsNullOrEmpty(userId))
  82. {
  83. obj.UpdateSysUserId = int.Parse(userId);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }