您实际上不能将字段附加到内容类型。当您将其附加到管理 UI 中的内容类型时,Orchard 会在幕后使用一些魔法来隐藏这个事实——它会在该内容类型中创建一个与内容类型同名的内容部分,然后附加该新内容部分的字段。
您可以通过管理 UI 附加一个字段来验证这一点,然后转到导入/导出,并为您的内容类型导出元数据。
要通过迁移附加字段,请执行相同的操作。如果您没有作为附加字段的好地方的内容部分,我使用的约定是创建一个与内容类型同名的内容部分,错误后缀为“部分”。假设您的内容类型是“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()
);
所有字段都附加到部件,而不是类型,因此您自然可以使用这种迁移方法使用placement.info 和模板来控制放置,就像您通过 UI 定义字段一样。