using CZFW.Core; using CZFW.Core.Security; using CZFW.Framework.Model.Entity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; namespace CZFW.Framework.Model { public class CZDbContext : DbContext { //public CZDbContext(DbContextOptions options) : base(options) //{ //} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //optionsBuilder.UseSqlServer(@"data Source=114.55.54.52,1433\CZDB;database=CZKJ_WLGL;user id =sa;pwd=CZKJczkj2016"); //optionsBuilder.UseMySQL(DesEncrypt.Decrypt(ConfigHelper.GetValue("ConnectionStrings:Development"))); optionsBuilder.UseMySQL(ConfigHelper.GetValue("ConnectionStrings:Development")); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { string path = Thread.GetDomain().BaseDirectory; CZHttpContext.Logger.LogError($"当前目录:{path}"); var scanPath = ConfigHelper.Configs.GetValue("EFScanPath"); path = path + scanPath; CZHttpContext.Logger.LogError($"即将扫描路径:{path}"); Assembly[] assemblies = new DirectoryInfo(path).GetFiles("CZ*.dll", SearchOption.AllDirectories) .Select(x => Assembly.LoadFrom(x.FullName)).ToArray(); Type baseType = typeof(EntityBase); foreach (var assembly in assemblies) { IEnumerable entityTypes = assembly.GetTypes().Where(entityType => entityType.IsSubclassOf(baseType)); foreach (var type in entityTypes) { modelBuilder.Entity(type); } } base.OnModelCreating(modelBuilder); } } }