gpt4 book ai didi

orchardcms - Orchard CMS 模块开发工作流程

转载 作者:行者123 更新时间:2023-12-03 11:16:53 24 4
gpt4 key购买 nike

我如何强制 Orchard 重新运行 Migrations.Create 方法来重新创建我的模块的数据?我看过的所有教程都使用 Migrations.UpdateFrom 方法让 Orchard 识别模块数据更改,但这意味着我在开发过程中每次进行更改时都必须创建一个新方法。这些方法对实际版本更新有意义,但对初始开发没有意义。

我试过禁用并重新启用该模块,没有成功。我也尝试卸载并重新安装该模块,但运行卸载命令从我的计算机中永久删除了该模块,绕过了回收站。

最佳答案

我对你的问题有点困惑,但我会尽力回答

迁移更新数据库,以及数据库中的数据。如果您想更改您在仪表板中看到的 View ,您不需要重新运行迁移。

但是,如果您想更改模型和数据,则需要运行更多迁移或重置数据库并运行创建迁移。

当我重建我正在处理的模块时,它将运行任何未完成的迁移,而不必在我的本地版本上禁用/启用该模块,

也就是说如果当前迁移版本是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();

}
}

关于orchardcms - Orchard CMS 模块开发工作流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com