在Orchard Cms导入只是导入最后一条记录

时间:2013-11-22 15:28:27

标签: xml import sql-server-ce orchardcms orchardcms-1.7

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

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

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

日志中没有任何错误。

我正在使用来自git repository的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)
            );

1 个答案:

答案 0 :(得分:3)

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

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

如果您想了解更多相关信息,请参阅DataRecipeHandler第81行。