1

我正在尝试将数据导入 Orchard CMS。我有一个自定义部分,我在驱动程序中覆盖了导入方法,并且我正在使用导入/导出模块。

我已经从 Orchard 导出了一些数据以确保 XML 模式正确,但是在导入时只导入最后一条记录,无论上面有什么数据。

我还打乱了 xml 中的记录,唯一导入的记录始终是 Id 不为空的最后一条记录。所以我可以说这不是有效或无效数据的问题。所有记录都是有效的,如果它们有一个 id(将被下一个自动生成的键替换)并且它们是集合中的最后一个,它们将被导入。

日志中没有任何错误。

我正在使用来自 git 存储库的 Orchard 1.7.2.0。

我使用 SQL compact 作为数据库引擎。

知道为什么会失败吗?

这是部分记录:

public class VehiclePartRecord : ContentPartRecord
{
    public virtual string Name { get; set; }
    public virtual byte VehicleRole { get; set; }
    public virtual string Identification { get; set; }
    public virtual string RadioCode { get; set; }
    public virtual int StartKm { get; set; }
    public virtual string Note { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime EndDate { get; set; }
    public virtual bool Active { get; set; }
}

这是驱动程序:

    protected override void Importing(VehiclePart part, ImportContentContext context)
    {
        var name = context.Attribute(part.PartDefinition.Name, "Name");
        if (name != null) {
            part.Name = name;
        }
        var vehicleRole = context.Attribute(part.PartDefinition.Name, "VehicleRole");
        if (!string.IsNullOrWhiteSpace(vehicleRole)) {
            part.VehicleRole = byte.Parse(vehicleRole);
        }
        var identification = context.Attribute(part.PartDefinition.Name, "Identification");
        if (!string.IsNullOrWhiteSpace(identification)) {
            part.Identification = identification.TrimEnd();
        }
        var radioCode = context.Attribute(part.PartDefinition.Name, "RadioCode");
        if (!string.IsNullOrWhiteSpace(radioCode)) {
            part.RadioCode = radioCode.TrimEnd();
        }
        var startKm = context.Attribute(part.PartDefinition.Name, "StartKm");
        if (!string.IsNullOrWhiteSpace(startKm)) {
            part.StartKm = int.Parse(startKm);
        }
        var note = context.Attribute(part.PartDefinition.Name, "Note");
        if (note != null) {
            part.Note = note;
        }
        var startDate = context.Attribute(part.PartDefinition.Name, "StartDate");
        if (!string.IsNullOrWhiteSpace(startDate)) {
            part.StartDate = DateTime.Parse(startDate);
        }
        var endDate = context.Attribute(part.PartDefinition.Name, "EndDate");
        if (!string.IsNullOrWhiteSpace(endDate)) {
            part.EndDate = DateTime.Parse(endDate);
        }
        var active = context.Attribute(part.PartDefinition.Name, "Active");
        if (!string.IsNullOrWhiteSpace(active)) {
            part.Active = bool.Parse(active);
        }
    }

这是我编辑过的导出文件,我正在尝试导入:

<!--Exported from Orchard-->
<Orchard>
    <Recipe>
        <Name>Generated by Orchard.ImportExport</Name>
        <Author>admin</Author>
        <ExportUtc>2013-11-22T11:39:39.9566929Z</ExportUtc>
    </Recipe>
    <Data>
        <Vehicle Id="4" Status="Published">
            <VehiclePart Name="Fiat Punto" VehicleRole="2" Identification="EL 999 LV" RadioCode="013" StartKm="0" Note="" StartDate="2008-04-12T00:00:00" EndDate="" Active="true" />
            <CommonPart Owner="/User.UserName=admin" CreatedUtc="2013-11-22T10:27:57.7066182Z" PublishedUtc="2013-11-22T10:27:57.7296195Z" ModifiedUtc="2013-11-22T10:27:57.7356199Z" />
        </Vehicle>
        <Vehicle Id="8" Status="Published">
            <VehiclePart Name="Fiat Punto" VehicleRole="2" Identification="EL 888 LV" RadioCode="014" StartKm="0" Note="" StartDate="2009-04-12T00:00:00" EndDate="" Active="true" />
            <CommonPart Owner="/User.UserName=admin" CreatedUtc="2013-11-22T10:27:57.7066182Z" PublishedUtc="2013-11-22T10:27:57.7296195Z" ModifiedUtc="2013-11-22T10:27:57.7356199Z" />
        </Vehicle>
        <Vehicle Id="12" Status="Published">
            <VehiclePart Name="Fiat Punto" VehicleRole="2" Identification="EL 777 LV" RadioCode="017" StartKm="0" Note="" StartDate="2010-03-02T00:00:00" EndDate="" Active="true" />
            <CommonPart Owner="/User.UserName=admin" CreatedUtc="2013-11-22T10:27:57.7066182Z" PublishedUtc="2013-11-22T10:27:57.7296195Z" ModifiedUtc="2013-11-22T10:27:57.7356199Z" />
        </Vehicle>
        <Vehicle Id="" Status="Published">
            <VehiclePart Name="Fiat Doblò" VehicleRole="2" Identification="DX 444 BL " RadioCode="051" StartKm="0" Note="" StartDate="2010-01-27T00:00:00" EndDate="" Active="true" />
            <CommonPart Owner="/User.UserName=admin" CreatedUtc="2013-11-22T10:27:57.7066182Z" PublishedUtc="2013-11-22T10:27:57.7296195Z" ModifiedUtc="2013-11-22T10:27:57.7356199Z" />
        </Vehicle>
    </Data>
</Orchard>

编辑:

根据请求,这是创建数据库结构的迁移代码

        SchemaBuilder.CreateTable(
            "VehiclePartRecord",
            table => table
                         .ContentPartRecord()
                         .Column<string>("Name", c => c.WithLength(25))
                         .Column("VehicleRole", DbType.Byte)
                         .Column<string>("Identification", c => c.WithLength(12))
                         .Column<string>("RadioCode", c => c.WithLength(5))
                         .Column<int>("StartKm")
                         .Column<string>("Note", c => c.WithLength(255))
                         .Column<DateTime>("StartDate")
                         .Column<DateTime>("EndDate")
                         .Column("Active", DbType.Boolean)
            );


        ContentDefinitionManager.AlterPartDefinition("VehiclePart",
                                                     builder => builder.Attachable());

        ContentDefinitionManager.AlterTypeDefinition(
            "Vehicle",
            cfg => cfg
                       .WithPart("VehiclePart")
                       .WithPart("CommonPart", p => p.WithSetting("OwnerEditorSettings.ShowOwnerEditor", "false"))
                       .Creatable(false)
            );
4

1 回答 1

3

我尝试了代码,问题出在id上。id 不应该为空,也应该是这样的:Id="/Identifier=3cf393fcbdea4c4a9e881e74ce177735

使用您的 id,字典始终具有相同的键,并且会覆盖最后一个键。我以前从未使用过导入/导出模块,但我猜这些 id 是你设置的。

如果您想了解更多信息,请查看 DataRecipeHandler 的第 81 行。

于 2013-11-26T03:41:37.057 回答