给定 Migrations 类的名称作为字符串,如何获取存储在 Orchard_Framework_DataMigrationRecord 中的当前版本号?
我可以在 IExtensionManager 中看到版本,但这似乎只是 module.txt 中定义的模块版本。
给定 Migrations 类的名称作为字符串,如何获取存储在 Orchard_Framework_DataMigrationRecord 中的当前版本号?
我可以在 IExtensionManager 中看到版本,但这似乎只是 module.txt 中定义的模块版本。
好的,所以我自己解决了这个问题-
我知道 Orchard 在触发迁移方法时一定已经在执行与我需要的代码类似的代码,因此我创建了一个新的迁移文件,并在 Create() 方法上放置了一个断点。当断点命中时,我通过调用堆栈查找 Orchard.Data.Migration 中的 DataMigrationManager。我需要的一切都在那里,如果其他人有类似的要求,我建议他们以该课程为起点。
这几乎是直接从该课程中提取出来的:
string moduleName="Your.Module.Name";
var migrations = GetDataMigrations(moduleName);
// apply update methods to each migration class for the module
var current = 0;
foreach (var migration in migrations)
{
// copy the objet for the Linq query
var tempMigration = migration;
// get current version for this migration
var dataMigrationRecord = GetDataMigrationRecord(tempMigration);
if (dataMigrationRecord != null)
{
current = dataMigrationRecord.Version.Value;
}
// do we need to call Create() ?
if (current == 0)
{
// try to resolve a Create method
var createMethod = GetCreateMethod(migration);
if (createMethod != null)
{
//create method has been written, but not executed!
current = (int)createMethod.Invoke(migration, new object[0]);
}
}
}
Context.Output.WriteLine("Version: {0}", current);
您可能需要的几种方法:
private DataMigrationRecord GetDataMigrationRecord(IDataMigration tempMigration)
{
return _dataMigrationRepository.Table
.Where(dm => dm.DataMigrationClass == tempMigration.GetType().FullName)
.FirstOrDefault();
}
private static MethodInfo GetCreateMethod(IDataMigration dataMigration)
{
var methodInfo = dataMigration.GetType().GetMethod("Create", BindingFlags.Public | BindingFlags.Instance);
if (methodInfo != null && methodInfo.ReturnType == typeof(int))
{
return methodInfo;
}
return null;
}
不要忘记注入您可能需要的任何依赖项。