将Migrations类的名称作为字符串,如何获取存储在Orchard_Framework_DataMigrationRecord中的当前版本号?
我可以在IExtensionManager中看到Version,但这看起来只是module.txt中定义的模块版本。
答案 0 :(得分:0)
好的,所以我自己解决了这个问题 -
我知道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;
}
不要忘记注入您可能需要的任何依赖项。