attempted to assign id from null one-to-one property: ContentItemRecord - Orchard CMS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



attempted to assign id from null one-to-one property: ContentItemRecord - Orchard CMS



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);





When did the error happen? Did you try to use IRepository to add the Order? :)
– wooncherk
Oct 7 '12 at 5:00





yes, I use it. I updated my post to show it.
– Max Zhukov
Oct 7 '12 at 9:53




3 Answers
3



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.





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 6:44





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; '
– AndyJ
Feb 17 '15 at 16:36



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





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 4 '13 at 6:12



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;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS