为什么 ContentItem 中不包含无字段 ContentPart?
以下是 Migrations.cs 中的代码:
SchemaBuilder.CreateTable("ImageDescribedPartRecord",
table => table.ContentPartRecord());
ContentDefinitionManager.AlterPartDefinition(
"ImageDescribedPart",
cpd => cpd.WithField(
"Image",
b => b
.OfType("MediaPickerField")
.WithSetting("MediaPickerFieldSettings.Required", "false")));
ContentDefinitionManager.AlterTypeDefinition(
"PlantPicture",
cfg => cfg
.WithPart("ImageDescribedPart")
.WithPart("CommonPart", p => p.WithSetting("OwnerEditorSettings.ShowOwnerEditor", "false"))
.WithPart("BodyPart")
.WithPart("TitlePart")
.WithPart("AutoroutePart"
, builder =>
builder
.WithSetting("AutorouteSettings.AllowCustomPattern", "false")
.WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "true")
.WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Container', Pattern: '{Content.Container.Path}/images/{Content.Slug}', Description: 'apgii/taxon/sub-taxon/images/title'}]")
.WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
.WithPart("ContainablePart")
.Creatable(true)
// todo: The following doesn't work. Make it work.
.WithSetting("BodyPartSettings.FlavorDefault", "text")
);
以下是 ContentPart 的代码:
public class ImageDescribedPart : ContentPart<ImageDescribedPartRecord>{
}
public class ImageDescribedPartRecord :ContentPartRecord {}
以下代码来自驱动程序
IContentQuery<ContentItem> query = _contentManager
.Query(VersionOptions.Published)
.Join<CommonPartRecord>()
.Where(cr => cr.Container.Id == container.Id);
var items = query.Slice(0, 10).ToList();
IEnumerable<Zulatm.WebPlants.Models.ImageDescribedPart> firstImages = items.AsPart<ImageDescribedPart>();
Logger.Debug("Items count: {0}", items.Count());
for (int i = 0; i < items.Count(); i++) {
Logger.Debug("Item {0}: {1}", i, items[i].As<TitlePart>().Title);
}
Logger.Debug("Images count: {0}",firstImages.Count());
返回以下内容
2012-12-07 16:28:45,616 [35] TaxonomyNodePartDriver - Items count: 2
2012-12-07 16:28:45,617 [35] TaxonomyNodePartDriver - Item 0: Test
2012-12-07 16:28:45,619 [35] TaxonomyNodePartDriver - Item 1: test img 2
2012-12-07 16:28:45,619 [35] TaxonomyNodePartDriver - Images count: 0