using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Reflection; using System.Runtime.Loader; using VOL.Core.DBManager; using VOL.Core.Extensions; using VOL.Core.Extensions.AutofacManager; using VOL.Entity.SystemModels; namespace VOL.Core.EFDbContext { public class VOLContext : DbContext, IDependency { /// /// 数据库连接名称 /// public string DataBaseName = null; public VOLContext() : base() { } public VOLContext(string connction) : base() { DataBaseName = connction; } public VOLContext(DbContextOptions options) : base(options) { } public override void Dispose() { base.Dispose(); } public override int SaveChanges() { try { return base.SaveChanges(); } catch (Exception ex)//DbUpdateException { throw (ex.InnerException as Exception ?? ex); } } public override DbSet Set() { return base.Set(); } //public DbSet Set(bool trackAll = false) where TEntity : class //{ // return base.Set(); //} /// /// 设置跟踪状态 /// public bool QueryTracking { set { this.ChangeTracker.QueryTrackingBehavior = value ? QueryTrackingBehavior.TrackAll : QueryTrackingBehavior.NoTracking; } } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connectionString = DBServerProvider.GetConnectionString(null); if (Const.DBType.Name == Enums.DbCurrentType.MySql.ToString()) { optionsBuilder.UseMySql(connectionString); } else if (Const.DBType.Name == Enums.DbCurrentType.PgSql.ToString()) { optionsBuilder.UseNpgsql(connectionString); } else { optionsBuilder.UseSqlServer(connectionString); } //默认禁用实体跟踪 optionsBuilder = optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); //var loggerFactory = new LoggerFactory(); //loggerFactory.AddProvider(new EFLoggerProvider()); // optionsBuilder.UseLoggerFactory(loggerFactory); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { Type type = null; try { //获取所有类库 var compilationLibrary = DependencyContext .Default .CompileLibraries .Where(x => !x.Serviceable && x.Type != "package" && x.Type == "project"); foreach (var _compilation in compilationLibrary) { //加载指定类 AssemblyLoadContext.Default .LoadFromAssemblyName(new AssemblyName(_compilation.Name)) .GetTypes() .Where(x => x.GetTypeInfo().BaseType != null && x.BaseType == (typeof(BaseEntity))) .ToList().ForEach(t => { modelBuilder.Entity(t); // modelBuilder.Model.AddEntityType(t); }); } //modelBuilder.AddEntityConfigurationsFromAssembly(GetType().Assembly); base.OnModelCreating(modelBuilder); } catch (Exception ex) { string mapPath = ($"Log/").MapPath(); Utilities.FileHelper.WriteFile(mapPath, $"syslog_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt", type?.Name + "--------" + ex.Message + ex.StackTrace + ex.Source); } } } }