CoreDbContext.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using ERP.Core.Entity;
  2. using ERP.Framework.WebApi;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Logging;
  6. using Serilog;
  7. using System.Reflection;
  8. namespace ERP.Core
  9. {
  10. public class CoreDbContext : DbContext
  11. {
  12. private readonly IConfiguration _configuration;
  13. public CoreDbContext(IConfiguration configuration)
  14. {
  15. _configuration = configuration;
  16. }
  17. public override int SaveChanges()
  18. {
  19. BeforeSaveChanges();
  20. var result = base.SaveChanges();
  21. AfterSaveChanges();
  22. return result;
  23. }
  24. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  25. {
  26. var name = GetType().Name;
  27. // 可扩展为多租户,从当前请求头拿租户Id,根据租户Id获取对应数据库配置名,替换
  28. string connect = _configuration.GetConnectionString(name)!;
  29. // 可扩展为使用多种数据库,添加配置项数组根据不同租户Id,配置不同库类型,Key为租户Id/库名,对象包含数据库类型等
  30. optionsBuilder.UseMySql(connect, new MySqlServerVersion(new Version(8, 0, 26)));
  31. UseLog(optionsBuilder);
  32. base.OnConfiguring(optionsBuilder);
  33. }
  34. /// <summary>
  35. /// 配置模型关系
  36. /// </summary>
  37. /// <param name="modelBuilder"></param>
  38. protected override void OnModelCreating(ModelBuilder modelBuilder)
  39. {
  40. modelBuilder.Entity<SysUserRole>().HasNoKey();
  41. modelBuilder.Entity<SysRoleMenu>().HasNoKey();
  42. var assembly = Assembly.GetExecutingAssembly();
  43. // 处理配置项
  44. modelBuilder.ApplyConfigurationsFromAssembly(assembly);
  45. foreach (Type type in assembly.ExportedTypes
  46. .Where(t => t.IsClass && (t.BaseType == typeof(BaseEntity) || t.BaseType == typeof(TreeEntity))))
  47. {
  48. modelBuilder.Model.AddEntityType(type);
  49. }
  50. }
  51. private void BeforeSaveChanges()
  52. {
  53. ChangeTracker.DetectChanges();
  54. var entityEntries = ChangeTracker.Entries().ToList();
  55. foreach (var entry in entityEntries)
  56. {
  57. // 新增时赋值CreatetIime CreateBy
  58. if (entry.State == EntityState.Added)
  59. {
  60. Entry(entry.Entity).Property(nameof(BaseEntity.CreateTime)).CurrentValue = DateTime.Now;
  61. //Entry(entry.Entity).Property(nameof(BaseEntity.CreateBy)).CurrentValue = DateTime.Now;
  62. }
  63. if (entry.State == EntityState.Deleted)
  64. {
  65. }
  66. // 修改时赋值CreatetIime CreateBy
  67. if (entry.State == EntityState.Modified)
  68. {
  69. Entry(entry.Entity).Property(nameof(BaseEntity.UpdateTime)).CurrentValue = DateTime.Now;
  70. }
  71. }
  72. }
  73. private void AfterSaveChanges()
  74. {
  75. }
  76. private void UseLog(DbContextOptionsBuilder optionsBuilder)
  77. {
  78. optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder =>
  79. {
  80. builder.AddConsole();
  81. builder.AddSerilog();
  82. }));
  83. }
  84. }
  85. }