/// <summary> /// 解析持久化模型。 /// </summary> /// <param name="dbContextAccessor">给定的数据库上下文访问器。</param> /// <returns>返回 <see cref="IModel"/>。</returns> protected virtual IModel ResolvePersistentModel (DataDbContextAccessor <TAudit, TAuditProperty, TMigration, TTabulation, TTenant, TGenId, TIncremId, TCreatedBy> dbContextAccessor) { return(ExtensionSettings.Preference.RunLocker(() => { var cacheKey = DbContextAccessorHelper.GetMigrationCacheKey(dbContextAccessor); return MemoryCache.GetOrCreate(cacheKey, entry => { Type snapshotType = null; // 启用写入分离后,数据库可能会主从同步,因此尝试从数据库获取迁移数据不作连接限制 var lastMigration = dbContextAccessor.Migrations .FirstOrDefaultByMax(s => s.CreatedTimeTicks); if (lastMigration.IsNotNull()) { var buffer = ModelSnapshotCompiler.RestoreAssembly(lastMigration.ModelBody); var modelAssembly = Assembly.Load(buffer); snapshotType = modelAssembly.GetType(lastMigration.ModelSnapshotName, throwOnError: true, ignoreCase: false); } if (snapshotType.IsNotNull()) { return snapshotType.EnsureCreate <ModelSnapshot>().Model; } return null; }); })); }
protected virtual (byte[] body, string hash) CreateModelSnapshot(DbContextAccessorBase dbContextAccessor, out TypeNameCombiner modelSnapshotTypeName) { var typeName = ModelSnapshotCompiler.GenerateTypeName(dbContextAccessor.CurrentType); var result = ExtensionSettings.Preference.RunLocker(() => { return(ModelSnapshotCompiler.CompileInMemory(dbContextAccessor, dbContextAccessor.Model, typeName)); }); modelSnapshotTypeName = typeName; return(result); }