向 Orchard DataMigration 中的 ContentType 添加字段 [英] Adding a Field to a ContentType in a Orchard DataMigration

查看:39
本文介绍了向 Orchard DataMigration 中的 ContentType 添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据迁移中创建了一个 ContentType,它将多个 ContentParts 焊接在一起.

I've created a ContentType in my data migration that welds several ContentParts together.

在 Orchard 网站内容管理中,我可以向 ContentType(但不是 ContentPart)添加一个字段,而在数据迁移中,似乎只能向 ContentPart(而不是 ContentType)添加一个字段.

On the Orchard Site Content Admin I can add a field to the ContentType (but not a ContentPart), and in the data migration it only seems possible to add a field to a ContentPart (and not a ContentType).

我想在迁移中将该字段添加到 ContentType,以便我可以使用placement.info 控制它的位置.

I would like to add the field to the ContentType in the migration, so I can control it's placement using placement.info.

也许这并不重要,还有另一种方法可以实现在迁移中添加字段,然后能够使用placement.info 控制它的放置位置以及使用模板的外观.

Perhaps that's not important, and there is another way to achieve adding a field in the migration and then being able to control where it is placed using placement.info and how it looks using a template.

推荐答案

您实际上无法将字段附加到内容类型.当您将其附加到管理 UI 中的内容类型时,Orchard 会在幕后使用一些魔法来向您隐藏这一事实——它会在该内容类型中创建一个与内容类型同名的内容部分,然后附加该新内容部分的字段.

You actually can't attach a field to a content type. When you attach it to a content type in the admin UI, Orchard does some magic behind the scenes to hide this fact from you -- it creates a content part inside that content type, with the same name as the content type, and then attaches the field(s) to that new content part.

您可以通过管理 UI 附加字段来验证这一点,然后转到导入/导出"并导出您的内容类型的元数据.

You can verify this by attaching a field via the admin UI, and then going to Import/Export, and exporting the metadata for your content types.

要通过迁移附加字段,请执行相同的操作.如果您没有适合附加字段的内容部分,我使用的约定是创建一个与内容类型同名的内容部分,错误后缀为部分".假设您的内容类型是VideoGame":

To attach a field via a Migration, do the same thing. If you don't have a content part that is a good place to attach the field, the convention I use is tocreate one with the same name as the Content Type, bug suffixed with "Part". So let's say your content type is "VideoGame":

ContentDefinitionManager.AlterPartDefinition(
    "VideoGamePart"
    , b => b
        .Attachable()
        .WithField("ThumbnailImage", cfg => cfg.OfType("MediaPickerField").WithDisplayName("Video game box cover image"))
);
// Type: 
ContentDefinitionManager.AlterTypeDefinition(
    "VideoGame"
    , cfg =>
        cfg
            .WithPart("VideoGamePart")
            .WithPart("IdentityPart")
            .WithPart("TitlePart")
            .WithPart("CommonPart")
            .Creatable()
);

所有字段都附加到部件,而不是类型,因此您自然可以使用这种迁移方法通过放置信息和模板控制放置,就像通过 UI 定义字段一样.

All fields are attached to Parts, not Types, so you naturally can control placement with placement.info and templates using this migration method, as you can if you define the field through the UI.

这篇关于向 Orchard DataMigration 中的 ContentType 添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆