/// <summary> /// 重写的方法 /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); var useDic = ConcurrentTypeDelDic.GetDic(); var thisType = this.GetType(); //使用委托 if (useDic.Contains(thisType)) { var tempPacker = useDic.Get(thisType); tempPacker.UseOnConfig?.Invoke(optionsBuilder); } }
/// <summary> /// 重写的方法 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var useDic = ConcurrentTypeDelDic.GetDic(); var thisType = this.GetType(); //使用委托 if (useDic.Contains(thisType)) { var tempPacker = useDic.Get(thisType); tempPacker.UseOnModelCreating?.Invoke(modelBuilder); } }
/// <summary> /// 制造上下文类型底层方法 /// </summary> /// <param name="useOneConfiguringDle">使用的定义委托</param> /// <param name="useOneMdoleCreatingDel">使用的模型定义委托</param> /// <param name="useKeyFilter">使用的Key值过滤器</param> /// <param name="useBaseType">使用的代理类基类</param> /// <returns>>创建的上下文类型</returns> private static Type CreatContextType ( OnModelCreatingDel useOneMdoleCreatingDel, IKEYFilter useKeyFilter, Type useBaseType) { List<Type> lstEntityTypes = new List<Type>(); List<Type> lstTempType; //加载当前应用程序域的所有程序集 foreach (var oneAssembliy in AppDomain.CurrentDomain.GetAssemblies()) { lstTempType = null; //尝试获取所有类型对象 try { lstTempType = oneAssembliy.GetTypes().ToList(); } catch (Exception) { lstTempType = new List<Type>(); } //寻找public的设置自动Entity的类型 foreach (var oneType in lstTempType) { AutoEntityAttribute tempAttribute = oneType.GetCustomAttribute(typeof(AutoEntityAttribute), false) as AutoEntityAttribute; if (oneType.IsClass && oneType.IsPublic && null != tempAttribute) { //使用Key过滤器过滤 if (null != useKeyFilter) { var tempString = tempAttribute.Key; //若需过滤则跳过 if (false == useKeyFilter.IfUse(tempString)) { continue; } } lstEntityTypes.Add(oneType); } } } //制造context代理对象 ContextEmitUtility tempUseEmitUtility = new ContextEmitUtility(m_strUseContextAssembly, m_strUseContext, lstEntityTypes); var returnValue = tempUseEmitUtility.EmiteContextType(useBaseType); if (null != returnValue) { //获取全局字典 ConcurrentTypeDelDic useDic = ConcurrentTypeDelDic.GetDic(); //制作临时封装 EFDelPacker tempPacker = new EFDelPacker() {UseOnModelCreating = useOneMdoleCreatingDel }; //添加到字典 useDic.Add(returnValue, tempPacker); } return returnValue; }