1

I'm writing the module for Orchard CMS.

There are several tables. In Migrations.cs:

It works fine:

            SchemaBuilder.CreateTable("CustomerPartRecord", table => table
            .ContentPartRecord()
            .Column<string>("FirstName", c => c.WithLength(50))
            .Column<string>("LastName", c => c.WithLength(50))
            .Column<string>("Title", c => c.WithLength(10))
            .Column<DateTime>("CreatedUtc")
            );

        ContentDefinitionManager.AlterPartDefinition("CustomerPart", part => part
            .Attachable(false)
            );

        ContentDefinitionManager.AlterTypeDefinition("Customer", type => type
            .WithPart("CustomerPart")
            .WithPart("UserPart")
            );
        return 6;

But it doesn't work:

            SchemaBuilder.CreateTable("OrderPartRecord", t => t
            .ContentPartRecord()
            .Column<int>("CustomerId", c => c.NotNull())
            .Column<DateTime>("CreatedAt", c => c.NotNull())
            .Column<decimal>("SubTotal", c => c.NotNull())
            .Column<string>("Status", c => c.WithLength(50).NotNull())
            );

        ContentDefinitionManager.AlterPartDefinition("OrderPart", part => part
             .Attachable(false)
        );

        ContentDefinitionManager.AlterTypeDefinition("Order", type => type
            .WithPart("OrderPart")
        );

There's error when I try to add Order. Error: attempted to assign id from null one-to-one property: ContentItemRecord What's wrong?

UPD:

Handler:

public class OrderPartHandler : ContentHandler
{
    public OrderPartHandler(IRepository<OrderPartRecord> repository)
    {
        Filters.Add(StorageFilter.For(repository));
    }
}

And creating Order:

_orderRepository.Create(order);
Max Zhukov
  • 877
  • 1
  • 8
  • 32

3 Answers3

6

You should never create a record yourself for a part record. If it's meant to be created without a content item, make it a regular record, not a content part record. Content part records need to always be associated with a content item and be created through the content manager, not the repository directly.

Bertrand Le Roy
  • 17,731
  • 2
  • 27
  • 33
  • Thanks, your answer clarified a lot things to me actually. – Petr Abdulin Oct 21 '13 at 11:54
  • Ok thanks. But I'd like to point out that you're not being very nice. On SO, the best contents rises to the top as people vote it up. It's not begging to encourage good practices. I hardly need the votes for myself. – Bertrand Le Roy Oct 22 '13 at 21:27
  • Well, sorry, I admit that was rude. However "vote up" comment also feels so, esp. if you already voted up. Peace :) – Petr Abdulin Oct 23 '13 at 06:44
  • 1
    For anyone wondering, a "regular" record is a record with no base type. You'll need to add the ID property yourself since it resided in the ContentPartRecord before. 'public virtual int Id { get; set; }' –  Feb 17 '15 at 16:36
2

As I've guessed, you are trying to create an Order using the repository, which results in the error if you don't take enough care. To solve the problem, use the ContentManager to create the order since Order is actually a content item.

PS:/ This error is related to nHibernate, specifically regarding the association between entites. Read this link for more information: Hibernate : attempted to assign id from null one-to-one property: employee

Community
  • 1
  • 1
wooncherk
  • 1,364
  • 7
  • 10
  • It would be great if someone actually documented the ContentManager though. The only way to understand it is to either find a code snippet or dig through code. – Ben Power Jul 04 '13 at 06:12
0

actually you can solve this problem by assigning the missing parameter to your part record it just needs to fill the order.ContentPartRecord = your_content_item.Record;