Orchardcms Orchard CMS:如何在迁移时向内容类型添加分类字段?

Orchardcms Orchard CMS:如何在迁移时向内容类型添加分类字段?,orchardcms,taxonomy,Orchardcms,Taxonomy,我需要定义一个新的内容类型,在我的模块迁移中有一个分类字段。 我想我需要这样做: ContentDefinitionManager.AlterTypeDefinition("ContentTypeName", cfg => cfg .WithPart("TermsPart", builder => builder .WithSetting(... 但是我无法让它工作。多亏了你,我终于成功

我需要定义一个新的内容类型,在我的模块迁移中有一个分类字段。 我想我需要这样做:

ContentDefinitionManager.AlterTypeDefinition("ContentTypeName",
            cfg => cfg
                .WithPart("TermsPart", builder => builder
                    .WithSetting(...

但是我无法让它工作。

多亏了你,我终于成功了。 关于Orchard,需要了解的重要一点是,字段不能附加到内容类型。当您将其附加到管理UI中的内容类型时,Orchard在幕后做了一些魔术来隐藏这一事实,它在该内容类型内创建了一个与内容类型同名的内容部分,然后将字段附加到该新内容部分

因此,解决方案如下:

        //Create new table for the new part
        SchemaBuilder.CreateTable(typeof(SampleRecord).Name, table => table
            .ContentPartRecord()
            .Column("SampleColumn", DbType.String)
        );

        //Attach field to the new part
        ContentDefinitionManager.AlterPartDefinition(
            typeof(SamplePart).Name, 
            cfg => cfg
                .Attachable()
                .WithField("Topic", fcfg => fcfg
                    .OfType("TaxonomyField")
                    .WithDisplayName("Topic")
                    .WithSetting("Taxonomy", "Topics")
                    .WithSetting("LeavesOnly", "true")
                    .WithSetting("SingleChoice", "true")
                    .WithSetting("Required", "true"))
            );

        //Attach part to the new Content Type
        ContentDefinitionManager.AlterTypeDefinition("Sample",
                 cfg => cfg
                     .WithPart(typeof(SamplePart).Name
                ));
我创建了一个表,其中包含一个名为“SampleColumn”的列,并为名为“Topics”的分类法附加了一个字段“Topic”。
希望它能帮助其他人。

对于当前的Orchard版本(我使用的是1.9),这种方法似乎需要稍微修改一下,才能正确应用定义的设置。例如,使用
。当使用
时,带设置(“分类”、“主题”)
不起作用。带设置(“分类字段设置.分类”、“主题”)
起作用。