using Estsh.Core.Quartz.Model; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Estsh.Core.Quartz.EFContext { public class QuarzEFContext : DbContext { public QuarzEFContext(DbContextOptions option) : base(option) { var databaseCreator = this.GetService(); if (!databaseCreator.HasTables()) { databaseCreator.EnsureCreated(); } //初始化的时候创建数据库 //this.Database.EnsureCreated(); //判断是否有待迁移 //if (this.Database.GetPendingMigrations().Any()) //{ // Debug.WriteLine("检测到实体有改动,正在创建迁移..."); // //执行迁移 // this.Database.Migrate(); // Debug.WriteLine("迁移完成"); //} //this.GetService().AddProvider(new MyFilteredLoggerProvider()); //options } #region 添加操作时间 public override int SaveChanges() { AddTimestamps(); return base.SaveChanges(); } public override Task SaveChangesAsync(CancellationToken cancellationToken = default) { AddTimestamps(); return base.SaveChangesAsync(cancellationToken); } public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { AddTimestamps(); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } public override int SaveChanges(bool acceptAllChangesOnSuccess) { AddTimestamps(); return base.SaveChanges(acceptAllChangesOnSuccess); } private void AddTimestamps() { var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseModel && (x.State == EntityState.Added || x.State == EntityState.Modified)); foreach (var entity in entities) { if (entity.State == EntityState.Added) { ((BaseModel)entity.Entity).timeflag = DateTime.Now; ((BaseModel)entity.Entity).changetime = DateTime.Now; } ((BaseModel)entity.Entity).changetime = DateTime.Now; } } #endregion /// /// 配置加载 /// /// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { } /// /// 实体创建 /// /// protected override void OnModelCreating(ModelBuilder modelBuilder) { var ddd = modelBuilder.Model.GetEntityTypes().ToList(); foreach (var item in ddd) { var tabtype = Type.GetType(item.ClrType.FullName); var props = tabtype.GetProperties(); var descriptionAttrtable = tabtype.GetCustomAttributes(typeof(DescriptionAttribute), true); if (descriptionAttrtable.Length > 0) { modelBuilder.Entity(item.Name).HasComment(((DescriptionAttribute)descriptionAttrtable[0]).Description); } foreach (var prop in props) { var descriptionAttr = prop.GetCustomAttributes(typeof(DescriptionAttribute), true); if (descriptionAttr.Length > 0) { modelBuilder.Entity(item.Name).Property(prop.Name).HasComment(((DescriptionAttribute)descriptionAttr[0]).Description); } } } } public DbSet tab_quarz_task { get; set; } public DbSet tab_quarz_tasklog { get; set; } } }