protected virtual void MigrateAspectServices(DataDbContextAccessor <TAudit, TAuditProperty, TMigration, TTabulation, TTenant, TGenId, TIncremId, TCreatedBy> dbContextAccessor, Action migrateStructureAction) { IServicesManager <IMigrateAccessorAspect> aspects = null; // 数据迁移支持写入连接(包括未启用读写分离的默认连接)// 或启用数据同步的默认与写入连接(数据同步改为在 AccessorBatchExecutor 底层实现) if (dbContextAccessor.IsWritingConnectionString()) // || dbContextAccessor.CurrentTenant.DataSynchronization { aspects = dbContextAccessor.GetService <IServicesManager <IMigrateAccessorAspect> >(); aspects.ForEach(aspect => { if (aspect.Enabled) { aspect.PreProcess(dbContextAccessor); // 前置处理数据迁移 } }); } // 结构迁移支持写入连接(包括未启用读写分离的默认连接)或启用结构同步的默认与写入连接 if (dbContextAccessor.IsWritingConnectionString() || dbContextAccessor.CurrentTenant.StructureSynchronization) { migrateStructureAction.Invoke(); } if (aspects.IsNotNull()) { aspects.ForEach(aspect => { if (aspect.Enabled) { aspect.PostProcess(dbContextAccessor); // 后置处理数据迁移 } }); } }
protected virtual void AddMigration (DataDbContextAccessor <TAudit, TAuditProperty, TMigration, TTabulation, TTenant, TGenId, TIncremId, TCreatedBy> dbContextAccessor) { if (!dbContextAccessor.IsWritingConnectionString()) { return; } (var body, var hash) = CreateModelSnapshot(dbContextAccessor, out var typeName); dbContextAccessor.MigrationsManager.TryAdd(p => p.ModelHash == hash, () => { var identifierGenerator = (IDataStoreIdentificationGenerator <TGenId>)dbContextAccessor .GetService <IStoreIdentificationGenerator>(); var migration = ObjectExtensions.EnsureCreate <TMigration>(); migration.Id = identifierGenerator.GenerateMigrationId(); migration.PopulateCreation(identifierGenerator.Clock); migration.AccessorName = dbContextAccessor.CurrentType.GetDisplayNameWithNamespace(); migration.ModelSnapshotName = typeName; migration.ModelBody = body; migration.ModelHash = hash; return(migration); }, addedPost => { if (!dbContextAccessor.RequiredSaveChanges) { dbContextAccessor.RequiredSaveChanges = true; } // 移除当前缓存 var cacheKey = DbContextAccessorHelper.GetMigrationCacheKey(dbContextAccessor); MemoryCache.Remove(cacheKey); // 发送迁移通知 var mediator = dbContextAccessor.GetService <IMediator>(); mediator.Publish(new MigrationNotification <TMigration> { Migration = addedPost.Entity }) .ConfigureAwaitCompleted(); }); }