public override async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { DatabaseToolkitEntities mySenparcEntities = serviceProvider.GetService <DatabaseToolkitEntities>(); //指定需要删除的数据实体 var dropTableKeys = EntitySetKeys.GetEntitySetInfo(this.XncfDatabaseDbContextType).Keys.ToArray(); //删除数据库表 await base.DropTablesAsync(serviceProvider, mySenparcEntities, dropTableKeys); await base.UninstallAsync(serviceProvider, unsinstallFunc).ConfigureAwait(false); }
/// <summary> /// 删除模块时需要执行的业务 /// </summary> /// <param name="serviceProvider"></param> /// <param name="unsinstallFunc"></param> /// <returns></returns> public override async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { MySenparcEntities mySenparcEntities = serviceProvider.GetService <MySenparcEntities>(); //指定需要删除的数据实体 //注意:这里作为演示,删除了所有的表,实际操作过程中,请谨慎操作,并且按照删除顺序对实体进行排序! var dropTableKeys = EntitySetKeys.GetEntitySetInfo(this.XscfDatabaseDbContextType).Keys.ToArray(); await base.DropTablesAsync(serviceProvider, mySenparcEntities, dropTableKeys); await unsinstallFunc().ConfigureAwait(false); }
public override async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { var mySenparcEntitiesType = this.TryGetXncfDatabaseDbContextType; XncfBuilderEntities mySenparcEntities = serviceProvider.GetService(mySenparcEntitiesType) as XncfBuilderEntities; var xncfDbContextType = MultipleDatabasePool.Instance.GetXncfDbContextType(this.GetType()); //指定需要删除的数据实体 var dropTableKeys = EntitySetKeys.GetEntitySetInfo(xncfDbContextType).Keys.ToArray(); //删除数据库表 await base.DropTablesAsync(serviceProvider, mySenparcEntities, dropTableKeys); await base.UninstallAsync(serviceProvider, unsinstallFunc).ConfigureAwait(false); }
public override async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { #region 除数据库(演示) var mySenparcEntitiesType = this.TryGetXncfDatabaseDbContextType; MessageSenparcEntities mySenparcEntities = serviceProvider.GetService(mySenparcEntitiesType) as MessageSenparcEntities; //指定需要删除的数据实体 //注意:这里作为演示,在卸载模块的时候删除了所有本模块创建的表,实际操作过程中,请谨慎操作,并且按照删除顺序对实体进行排序! var dropTableKeys = EntitySetKeys.GetEntitySetInfo(this.TryGetXncfDatabaseDbContextType).Keys.ToArray(); await base.DropTablesAsync(serviceProvider, mySenparcEntities, dropTableKeys); #endregion await unsinstallFunc().ConfigureAwait(false); }
public async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { //TODO:可以在基础模块里给出选项是否删除 WeixinSenparcEntities mySenparcEntities = serviceProvider.GetService <WeixinSenparcEntities>(); //指定需要删除的数据实体 //注意:这里作为演示,删除了所有的表,实际操作过程中,请谨慎操作,并且按照删除顺序对实体进行排序! var dropTableKeys = EntitySetKeys.GetEntitySetInfo(this.XscfDatabaseDbContextType).Keys.ToList(); //按照删除顺序排序 var types = new[] { typeof(UserTag_WeixinUser), typeof(UserTag), typeof(WeixinUser), typeof(MpAccount) }; types.ToList().AddRange(dropTableKeys); types = types.Distinct().ToArray(); await base.DropTablesAsync(serviceProvider, mySenparcEntities, types); await base.UninstallAsync(serviceProvider, unsinstallFunc).ConfigureAwait(false); }
public override async Task UninstallAsync(IServiceProvider serviceProvider, Func <Task> unsinstallFunc) { //TODO:可以在基础模块里给出选项是否删除 #region 除数据库(演示) var mySenparcEntitiesType = this.TryGetXncfDatabaseDbContextType; WeixinSenparcEntities mySenparcEntities = serviceProvider.GetService(mySenparcEntitiesType) as WeixinSenparcEntities; //指定需要删除的数据实体 //注意:这里作为演示,在卸载模块的时候删除了所有本模块创建的表,实际操作过程中,请谨慎操作,并且按照删除顺序对实体进行排序! var dropTableKeys = EntitySetKeys.GetEntitySetInfo(this.TryGetXncfDatabaseDbContextType).Keys.ToArray(); //按照删除顺序排序 var types = new[] { typeof(UserTag_WeixinUser), typeof(UserTag), typeof(WeixinUser), typeof(MpAccount) }; types.ToList().AddRange(dropTableKeys); types = types.Distinct().ToArray(); //指定需要删除的数据实体 await base.DropTablesAsync(serviceProvider, mySenparcEntities, types); #endregion await base.UninstallAsync(serviceProvider, unsinstallFunc).ConfigureAwait(false); }
/// <summary> /// 自动添加所有 XNCF 模块中标记了 [XncfAutoConfigurationMapping] 特性的对象 /// </summary> public static void ApplyAllAutoConfigurationMapping(ModelBuilder modelBuilder) { var entityTypeConfigurationMethod = typeof(ModelBuilder).GetMethods() .FirstOrDefault(z => z.Name == "ApplyConfiguration" && z.ContainsGenericParameters && z.GetParameters().SingleOrDefault()?.ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>)); //所有模块中数据库实体中自动获取所有的 DbSet 下的实体类型 foreach (var databaseRegister in Register.XncfDatabaseList) { if (databaseRegister.XncfDatabaseDbContextType != null) { var setKeyInfoList = EntitySetKeys.GetEntitySetInfo(databaseRegister.XncfDatabaseDbContextType).Values; foreach (var setKeyInfo in setKeyInfoList) { //数据库实体类型 var entityType = setKeyInfo.DbSetType; //默认空 ConfigurationMapping 对象的泛型类型 var blankEntityTypeConfigurationType = typeof(BlankEntityTypeConfiguration <>).MakeGenericType(entityType); //创建一个新的实例 var blankEntityTypeConfiguration = Activator.CreateInstance(blankEntityTypeConfigurationType); //最佳到末尾,这样可以优先执行用户自定义的代码 XncfAutoConfigurationMappingList.Add(blankEntityTypeConfiguration); } } } foreach (var autoConfigurationMapping in XncfAutoConfigurationMappingList) { if (autoConfigurationMapping == null) { continue; } SenparcTrace.SendCustomLog("监测到 ApplyAllAutoConfigurationMapping 执行", autoConfigurationMapping.GetType().FullName); //获取配置实体类型,如:DbConfig_WeixinUserConfigurationMapping Type mappintConfigType = autoConfigurationMapping.GetType(); //获取 IEntityTypeConfiguration<Entity> 接口 var interfaceType = mappintConfigType.GetInterfaces().FirstOrDefault(z => z.Name.StartsWith("IEntityTypeConfiguration")); if (interfaceType == null) { continue; } //实体类型,如:DbConfig Type entityType = interfaceType.GenericTypeArguments[0]; if (ApplyedAutoConfigurationMappingTypes.Contains(entityType)) { continue;//如果已经添加过则跳过。作此判断因为:原始的 XncfAutoConfigurationMappingList 数据可能和上一步自动添加 DataSet 中的对象有重复 } entityTypeConfigurationMethod.MakeGenericMethod(entityType) .Invoke(modelBuilder, new object[1] { autoConfigurationMapping }); ApplyedAutoConfigurationMappingTypes.Add(entityType); //entityTypeConfigurationMethod.Invoke(modelBuilder, new[] { autoConfigurationMapping }); } //TODO:添加 IQueryTypeConfiguration<> }
/// <summary> /// 自动添加所有 XNCF 模块中标记了 [XncfAutoConfigurationMapping] 特性的对象 /// </summary> public static void ApplyAllAutoConfigurationMapping(ModelBuilder modelBuilder) { var entityTypeConfigurationMethod = typeof(ModelBuilder).GetMethods() .FirstOrDefault(z => z.Name == "ApplyConfiguration" && z.ContainsGenericParameters && z.GetParameters().SingleOrDefault()?.ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>)); //所有模块中数据库实体中自动获取所有的 DbSet 下的实体类型 //TODO:筛选基础类中的 var xncfDatabaseDbContextList = MultipleDatabasePool.Instance.Values.SelectMany(z => z.Values); //TODO:为了进一步提高效率,可以对同一个 DbContext 的子类去重 foreach (var xncfDatabaseDbContextType in xncfDatabaseDbContextList) { var setKeyInfoList = EntitySetKeys.GetEntitySetInfo(xncfDatabaseDbContextType).Values; foreach (var setKeyInfo in setKeyInfoList) { //数据库实体类型 var entityType = setKeyInfo.DbSetType; if (AddedApplyedAutoConfigurationMappingEntityTypes.Contains(entityType)) { //Console.WriteLine($"\t [{xncfDatabaseDbContextType.Name}]ApplyAllAutoConfigurationMapping 有重复 setKeyInfo:{entityType.Name},已跳过"); continue; } //Console.WriteLine($"\t [{xncfDatabaseDbContextType.Name}]ApplyAllAutoConfigurationMapping 处理 setKeyInfo:{entityType.Name}"); //默认空 ConfigurationMapping 对象的泛型类型 var blankEntityTypeConfigurationType = typeof(BlankEntityTypeConfiguration <>).MakeGenericType(entityType); //创建一个新的实例 var blankEntityTypeConfiguration = Activator.CreateInstance(blankEntityTypeConfigurationType); //最佳到末尾,这样可以优先执行用户自定义的代码 XncfAutoConfigurationMappingList.Add(blankEntityTypeConfiguration); AddedApplyedAutoConfigurationMappingEntityTypes.Add(entityType); } } foreach (var autoConfigurationMapping in XncfAutoConfigurationMappingList) { if (autoConfigurationMapping == null) { continue; } SenparcTrace.SendCustomLog("监测到 ApplyAllAutoConfigurationMapping 执行", autoConfigurationMapping.GetType().FullName); //获取配置实体类型,如:DbConfig_WeixinUserConfigurationMapping Type mappintConfigType = autoConfigurationMapping.GetType(); //获取 IEntityTypeConfiguration<Entity> 接口 var interfaceType = mappintConfigType.GetInterfaces().FirstOrDefault(z => z.Name.StartsWith("IEntityTypeConfiguration")); if (interfaceType == null) { continue; } //实体类型,如:DbConfig Type entityType = interfaceType.GenericTypeArguments[0]; //PS:此处不能过滤,否则可能导致如 SystemServiceEntities_SqlServer / SystemServiceEntities_Mysql 中只有先注册的对象才成功,后面的被忽略 //if (ApplyedAutoConfigurationMappingTypes.Contains(entityType)) //{ // //如果已经添加过则跳过。作此判断因为:原始的 XncfAutoConfigurationMappingList 数据可能和上一步自动添加 DataSet 中的对象有重复 // //continue; //} entityTypeConfigurationMethod.MakeGenericMethod(entityType) .Invoke(modelBuilder, new object[1] { autoConfigurationMapping }); ApplyedAutoConfigurationMappingTypes.Add(entityType); //entityTypeConfigurationMethod.Invoke(modelBuilder, new[] { autoConfigurationMapping }); } //TODO:添加 IQueryTypeConfiguration<> }