1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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<string>("ConnectionStrings:Development")));
- optionsBuilder.UseMySQL(ConfigHelper.GetValue<string>("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<string>("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<Type> entityTypes = assembly.GetTypes().Where(entityType => entityType.IsSubclassOf(baseType));
- foreach (var type in entityTypes)
- {
- modelBuilder.Entity(type);
- }
- }
- base.OnModelCreating(modelBuilder);
- }
- }
- }
|