如何强制Orchard重新运行Migrations.Create方法来重新创建模块的数据?我见过的所有教程都使用Migrations.UpdateFrom方法让Orchard识别模块数据更改,但这意味着每次在开发过程中进行更改时我都必须创建一个新方法。这些方法对于实际版本更新有意义,但不适用于初始开发。
我尝试过禁用和重新启用模块,没有骰子。我还尝试卸载并重新安装模块,但运行uninstall命令会永久删除计算机中的模块,绕过回收站。
答案 0 :(得分:3)
我对你的问题感到有点困惑,但我会尝试回答
迁移更新数据库和数据库中的数据。如果要对仪表板中显示的视图进行更改,则不应重新运行迁移。
但是,如果要更改模型和数据,则需要运行更多迁移或重置数据库并运行create migration。
当我重建我正在处理的模块时,它将运行任何未完成的迁移,而不必在我的本地版本上禁用/启用该模块,
也就是说,如果当前迁移版本为1并且存在UpdateFrom1方法,则在迁移中,这将在构建项目时运行。
您可以查看下表中的当前版本号Orchard_Framework_DataMigrationRecord
如果要强制重新运行迁移,可以重置数据库中表的值。或者,您可以备份数据库,并在运行整个迁移套件之前进行恢复。
在开发过程中,我使用简短的迁移,每次需要添加数据时创建一个方法,我会尽量保持这些小,以便在开发过程中轻松识别任何问题。
然后在完成模块之前,将迁移合并到2或3个逻辑代码块
以下是自定义类型的迁移
public int Create()
{
// Define the project type
ContentDefinitionManager.AlterTypeDefinition("Project", cfg => cfg
.WithSetting("Stereotype", "Content")
.CommomPart()
.AutoroutePart("our-work")
.BodyPart()
.WithPart("TitlePart")
.WithPart("PublishLaterPart")
.WithPart("MenuPart", builder => builder
.WithSetting("MenuPart.OnMenu", "true")
.WithSetting("MenuPart.CurrentMenu", "Project Menu"))
.WithPart("Project")
.Creatable()
.Draftable());
return 1;
}
public int UpdateFrom1()
{
// Define project part - having a part with the same name will create fields in the project type
ContentDefinitionManager.AlterPartDefinition("Project", builder => builder
.MediaPickerField("MainImage")
.MediaPickerField("MediumImage")
.MediaPickerField("SmallImage")
.MediaPickerField("Logo")
.TextField("ShortDescription", Flavour.TextArea)
.TextField("Features", Flavour.Markdown)
.TextField("ClientTitle", Flavour.DefaultFlavour)
.TextField("ClientName", Flavour.DefaultFlavour)
.TextField("ClientQuote", Flavour.Textarea)
.BooleanField("MainProjectOnHomePage", false)
.Attachable());
return 2;
}
我添加了一些扩展方法,以减少冗长。
这里是
public static class MigrationExtentionHelpers
{
// part definitions
public static ContentPartDefinitionBuilder MediaPickerField(this ContentPartDefinitionBuilder builder,
string name, bool required = true, string hint = "")
{
var displayName = SplitCamel(name);
// default implementation of Media picker field - create overloads for more options
return builder.WithField(name, fieldBuilder => fieldBuilder
.OfType("MediaPickerField")
.WithDisplayName(displayName)
.WithSetting("MediaPickerFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
.WithSetting("MediaPickerFieldSettings.AllowedExtensions", "jpg png gif")
.WithSetting("MediaPickerFieldSettings.Hint", hint));
}
public static ContentPartDefinitionBuilder TextField(this ContentPartDefinitionBuilder builder,
string name, Flavour flavor, bool required = true, string hint = "")
{
var strFlavor = SplitCamel(flavor.ToString());
// default implementation of Media picker field - create overloads for more options
return builder.WithField(name, fieldBuilder => fieldBuilder
.OfType("TextField")
.WithSetting("TextFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
.WithSetting("TextFieldSettings.Flavor", strFlavor)
.WithSetting("TextFieldSettings.Hint", hint));
}
public static ContentPartDefinitionBuilder BooleanField(this ContentPartDefinitionBuilder builder,
string name, bool defalut, string hint = "")
{
// default implementation of Media picker field - create overloads for more options
return builder.WithField(name, fieldBuilder => fieldBuilder
.OfType("BooleanField")
.WithSetting("BooleanFieldSettings.Hint", hint)
.WithSetting("BooleanFieldSettings.DefaultValue", defalut.ToString(CultureInfo.InvariantCulture)));
}
// type definitions
public static ContentTypeDefinitionBuilder AutoroutePart(this ContentTypeDefinitionBuilder builder, string pathPrefix = "")
{
var pattern = string.Format("[{{Name:'{0}/Title', Pattern: '{0}/{{Content.Slug}}', Description: 'my-page'}}]", pathPrefix);
return builder.WithPart("AutoroutePart", partBuilder => partBuilder
.WithSetting("AutorouteSettings.PatternDefinitions", pattern));
}
public static ContentTypeDefinitionBuilder BodyPart(this ContentTypeDefinitionBuilder builder,
Flavour defaultFlavour = Flavour.Markdown)
{
return builder.WithPart("BodyPart", partBuilder => partBuilder
.WithSetting("BodyTypePartSettings.Flavor", defaultFlavour.ToString()));
}
public static ContentTypeDefinitionBuilder CommomPart(this ContentTypeDefinitionBuilder builder)
{
return builder.WithPart("CommonPart")
.WithSetting("OwnerEditorSettings.ShowOwnerEditor", false.ToString(CultureInfo.InvariantCulture).ToLower());
}
private static string SplitCamel(string enumString)
{
StringBuilder sb = new StringBuilder();
char last = char.MinValue;
foreach (char c in enumString)
{
if (char.IsLower(last) && char.IsUpper(c))
{
sb.Append(' ');
sb.Append(c.ToString(CultureInfo.InvariantCulture).ToLower());
}
else
{
sb.Append(c);
}
last = c;
}
return sb.ToString();
}
}
答案 1 :(得分:0)
除非您从之前创建的备份中恢复数据库或直接调整数据,否则基本上无法运行两次迁移。 至于我,这不是问题,因为在我的团队开发过程中,我们使用dbs的本地实例,并将所有迁移组合成初始迁移并部署到测试/暂存环境。