我在导入某种内容类型方面遇到了问题。如果我导出单个项目然后导回它不仅不匹配并更新现有项目,它甚至还会创建第二个完全空的项目。
Screenshot of duplicate content item
我在内容部分中使用了自定义标识属性,这可能是问题所在,但我会认为即使它不能与现有项目匹配,也不应该这样做。要创建空的内容项。
该部件的处理程序会覆盖GetItemMetadata以添加标识:
protected override void GetItemMetadata(GetContentItemMetadataContext context)
{
var part = context.ContentItem.As<ProductPart>();
if (part != null)
{
context.Metadata.Identity.Add("PartNumber", part.PartNumber);
}
}
导入和导出功能在驱动程序中定义如下:
protected override void Importing(ProductPart part, ImportContentContext context)
{
// Don't do anything if the tag is not specified.
if (context.Data.Element(part.PartDefinition.Name) == null)
{
return;
}
context.ImportAttribute(part.PartDefinition.Name, "PartNumber", partNumber =>
part.PartNumber = partNumber
);
}
protected override void Exporting(ProductPart part, ExportContentContext context)
{
context.Element(part.PartDefinition.Name).SetAttributeValue("PartNumber", part.PartNumber);
}
这是我用于测试的导出XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Exported from Orchard-->
<Orchard>
<Recipe>
<ExportUtc>2016-07-26T14:03:45.9022599Z</ExportUtc>
</Recipe>
<Content>
<Product Id="/PartNumber=BMF 20 20 30" Status="Published">
<EnumerationField.Units Value="Metric" />
<NumericField.InnerDiameter Value="0.19" />
<NumericField.Length Value="0.185" />
<NumericField.OuterDiameter Value="0.5" />
<TaxonomyField.ProductRange Terms="/alias=taxonomies\/product-ranges\/oilite/Identifier=/Reference=oilite" />
<TaxonomyField.ProductType Terms="/alias=taxonomies\/product-types\/plain/Identifier=/Reference=plain" />
<CommonPart Owner="/User.UserName=admin" CreatedUtc="2016-07-26T14:03:27.8841625Z" PublishedUtc="2016-07-26T14:03:27.9597274Z" ModifiedUtc="2016-07-26T14:03:27.9547403Z" />
<ProductPart PartNumber="BMF 20 20 30" />
</Product>
</Content>
</Orchard>
除了PartNumber之外的所有属性都被定义为Fields,因此PartNumber是唯一进入数据库中自己的表的东西。
该部分的完整定义如下:
SchemaBuilder.CreateTable("ProductPartRecord", table => table
.ContentPartRecord()
.Column<string>("PartNumber", column => column
.WithLength(50)
)
);
ContentDefinitionManager.AlterPartDefinition("ProductPart", builder => builder
.Attachable()
.WithDescription("Turns a content type into a Product with a Part Number."));
ContentDefinitionManager.AlterPartDefinition("ProductPart", builder => builder
.WithField("ProductRange", cfg => cfg
.OfType("TaxonomyField")
.WithDisplayName("Material/Range")
.WithSetting("TaxonomyFieldSettings.Taxonomy", "Product Ranges")
.WithSetting("TaxonomyFieldSettings.LeavesOnly", "True")
.WithSetting("TaxonomyFieldSettings.Required", "True")
.WithSetting("TaxonomyFieldSettings.SingleChoice", "True")
.WithSetting("TaxonomyFieldSettings.Autocomplete", "False")
.WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "False")
)
.WithField("ProductType", cfg => cfg
.OfType("TaxonomyField")
.WithDisplayName("Type/Form")
.WithSetting("TaxonomyFieldSettings.Taxonomy", "Product Types")
.WithSetting("TaxonomyFieldSettings.LeavesOnly", "True")
.WithSetting("TaxonomyFieldSettings.Required", "True")
.WithSetting("TaxonomyFieldSettings.SingleChoice", "True")
.WithSetting("TaxonomyFieldSettings.Autocomplete", "False")
.WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "False")
)
.WithField("Units", cfg => cfg
.OfType("EnumerationField")
.WithDisplayName("Units")
.WithSetting("EnumerationFieldSettings.Required", "True")
.WithSetting("EnumerationFieldSettings.Options", "Metric\r\nImperial")
.WithSetting("EnumerationFieldSettings.ListMode", "Radiobutton")
)
.WithField("InnerDiameter", cfg => cfg
.OfType("NumericField")
.WithDisplayName("Inner Diameter")
.WithSetting("NumericFieldSettings.Scale", "3")
)
.WithField("OuterDiameter", cfg => cfg
.OfType("NumericField")
.WithDisplayName("Outer Diameter")
.WithSetting("NumericFieldSettings.Scale", "3")
)
.WithField("Length", cfg => cfg
.OfType("NumericField")
.WithDisplayName("Length")
.WithSetting("NumericFieldSettings.Scale", "3")
)
.WithField("Thickness", cfg => cfg
.OfType("NumericField")
.WithDisplayName("Thickness")
.WithSetting("NumericFieldSettings.Scale", "3")
)
.WithField("Width", cfg => cfg
.OfType("NumericField")
.WithDisplayName("Width")
.WithSetting("NumericFieldSettings.Scale", "3")
)
.WithField("SurfaceStyle", cfg => cfg
.OfType("EnumerationField")
.WithDisplayName("Surface Style")
.WithSetting("EnumerationFieldSettings.Options", "\r\nPlain\r\nSmooth\r\nHoles\r\nDiamond Pockets\r\nRound Pockets")
.WithSetting("EnumerationFieldSettings.ListMode", "Dropdown")
)
.WithField("BackingMaterial", cfg => cfg
.OfType("EnumerationField")
.WithDisplayName("Backing Material")
.WithSetting("EnumerationFieldSettings.ListMode", "Dropdown")
.WithSetting("EnumerationFieldSettings.Options", "\r\nMild Steel\r\nBronze Backing")
)
);
非常感谢任何帮助!
答案 0 :(得分:1)
您需要为IIdentityResolverSelector
属性实施PartNumber
。这将使导入能够正确理解您的新身份。代码中有多个例子可以向您展示。